Skip to main content

qualia_core_db/net/qdnf/fabric/
join.rs

1//! Plane join prohibition and typed evidence-relation validation (IDF-02).
2//!
3//! No universal NaturalAgent join key exists: identity-equivalence and person-merge
4//! attempts fail closed (`QdnfError::Unauthorized | QdnfError::Denied`), whereas
5//! typed cross-plane evidence morphisms (`idf:supportedByInstrument`,
6//! `idf:assertedAbout`, `idf:observedAtHandle`) are explicitly permitted.
7
8use crate::net::qdnf::authority::Plane;
9use crate::net::qdnf::errors::QdnfError;
10
11/// Proposed merge key. None of these may join planes or persons.
12#[repr(u8)]
13#[derive(Clone, Copy, Debug, PartialEq, Eq)]
14pub enum JoinKey {
15    Did = 1,
16    Wallet = 2,
17    Location = 3,
18    Role = 4,
19    Alias = 5,
20    ClassifierSimilarity = 6,
21    SameAsAssertion = 7,
22    BiometricSample = 8,
23    AccountHandle = 9,
24    SensorCoPresence = 10,
25    OrgRepresentative = 11,
26    RelianceRetraction = 12,
27}
28
29impl JoinKey {
30    pub const ALL: [Self; 12] = [
31        Self::Did,
32        Self::Wallet,
33        Self::Location,
34        Self::Role,
35        Self::Alias,
36        Self::ClassifierSimilarity,
37        Self::SameAsAssertion,
38        Self::BiometricSample,
39        Self::AccountHandle,
40        Self::SensorCoPresence,
41        Self::OrgRepresentative,
42        Self::RelianceRetraction,
43    ];
44}
45
46/// Bounded join request. Copy-only; no string aliases on the hot path.
47#[repr(C)]
48#[derive(Clone, Copy, Debug, PartialEq, Eq)]
49pub struct JoinAttempt {
50    pub left: Plane,
51    pub right: Plane,
52    pub key: JoinKey,
53}
54
55/// Every join-key attempt is refused. Similarity and possession are not identity.
56#[inline]
57pub fn attempt_join(attempt: JoinAttempt) -> Result<(), QdnfError> {
58    let _ = (attempt.left, attempt.right);
59    match attempt.key {
60        JoinKey::Did
61        | JoinKey::Wallet
62        | JoinKey::Role
63        | JoinKey::BiometricSample
64        | JoinKey::AccountHandle
65        | JoinKey::OrgRepresentative => Err(QdnfError::Unauthorized),
66        JoinKey::Location
67        | JoinKey::Alias
68        | JoinKey::ClassifierSimilarity
69        | JoinKey::SameAsAssertion
70        | JoinKey::SensorCoPresence
71        | JoinKey::RelianceRetraction => Err(QdnfError::Denied),
72    }
73}
74
75/// First-class typed cross-plane evidence morphisms in `C_IDF` (ยง1.3.3).
76/// These link planes without collapsing or equating their subjects.
77#[repr(u8)]
78#[derive(Clone, Copy, Debug, PartialEq, Eq)]
79pub enum EvidenceRelation {
80    /// `idf:supportedByInstrument` (`Claim -> Instrument`)
81    SupportedByInstrument = 1,
82    /// `idf:assertedAbout` (`Claim -> Entity | Instrument | Handle`)
83    AssertedAbout = 2,
84    /// `idf:observedAtHandle` (`Claim | Instrument -> Handle`)
85    ObservedAtHandle = 3,
86}
87
88impl EvidenceRelation {
89    pub const IRI_SUPPORTED_BY_INSTRUMENT: &'static str = "urn:qualia:idf:supportedByInstrument";
90    pub const IRI_ASSERTED_ABOUT: &'static str = "urn:qualia:idf:assertedAbout";
91    pub const IRI_OBSERVED_AT_HANDLE: &'static str = "urn:qualia:idf:observedAtHandle";
92}
93
94/// Validate a typed cross-plane evidence relation. Never merges planes.
95#[inline]
96pub fn validate_evidence_relation(
97    relation: EvidenceRelation,
98    from: Plane,
99    to: Plane,
100) -> Result<(), QdnfError> {
101    match relation {
102        EvidenceRelation::SupportedByInstrument => {
103            if from == Plane::Claim && to == Plane::Instrument {
104                Ok(())
105            } else {
106                Err(QdnfError::Denied)
107            }
108        }
109        EvidenceRelation::AssertedAbout => {
110            if from == Plane::Claim
111                && matches!(to, Plane::Entity | Plane::Instrument | Plane::Handle | Plane::Claim)
112            {
113                Ok(())
114            } else {
115                Err(QdnfError::Denied)
116            }
117        }
118        EvidenceRelation::ObservedAtHandle => {
119            if matches!(from, Plane::Claim | Plane::Instrument) && to == Plane::Handle {
120                Ok(())
121            } else {
122                Err(QdnfError::Denied)
123            }
124        }
125    }
126}
127
128#[cfg(test)]
129mod tests {
130    use super::*;
131
132    const PLANES: [Plane; 4] = [
133        Plane::Entity,
134        Plane::Claim,
135        Plane::Handle,
136        Plane::Instrument,
137    ];
138
139    fn err_of(key: JoinKey) -> QdnfError {
140        attempt_join(JoinAttempt {
141            left: Plane::Entity,
142            right: Plane::Instrument,
143            key,
144        })
145        .unwrap_err()
146    }
147
148    #[test]
149    fn did_join_is_unauthorized() {
150        assert_eq!(err_of(JoinKey::Did), QdnfError::Unauthorized);
151    }
152
153    #[test]
154    fn wallet_join_is_unauthorized() {
155        assert_eq!(err_of(JoinKey::Wallet), QdnfError::Unauthorized);
156    }
157
158    #[test]
159    fn location_join_is_denied() {
160        assert_eq!(err_of(JoinKey::Location), QdnfError::Denied);
161    }
162
163    #[test]
164    fn role_join_is_unauthorized() {
165        assert_eq!(err_of(JoinKey::Role), QdnfError::Unauthorized);
166    }
167
168    #[test]
169    fn alias_join_is_denied() {
170        assert_eq!(err_of(JoinKey::Alias), QdnfError::Denied);
171    }
172
173    #[test]
174    fn classifier_similarity_join_is_denied() {
175        assert_eq!(err_of(JoinKey::ClassifierSimilarity), QdnfError::Denied);
176    }
177
178    #[test]
179    fn f1_extended_join_keys_are_refused() {
180        assert_eq!(err_of(JoinKey::SameAsAssertion), QdnfError::Denied);
181        assert_eq!(err_of(JoinKey::BiometricSample), QdnfError::Unauthorized);
182        assert_eq!(err_of(JoinKey::AccountHandle), QdnfError::Unauthorized);
183        assert_eq!(err_of(JoinKey::SensorCoPresence), QdnfError::Denied);
184        assert_eq!(err_of(JoinKey::OrgRepresentative), QdnfError::Unauthorized);
185        assert_eq!(err_of(JoinKey::RelianceRetraction), QdnfError::Denied);
186    }
187
188    #[test]
189    fn every_join_key_refuses_every_plane_pair() {
190        for &left in &PLANES {
191            for &right in &PLANES {
192                for key in JoinKey::ALL {
193                    let outcome = attempt_join(JoinAttempt { left, right, key });
194                    assert!(
195                        matches!(outcome, Err(QdnfError::Denied | QdnfError::Unauthorized)),
196                        "join {left:?}+{right:?} via {key:?} must not succeed"
197                    );
198                }
199            }
200        }
201    }
202
203    #[test]
204    fn typed_evidence_relations_permitted_only_for_valid_domain_and_range_planes() {
205        assert_eq!(
206            validate_evidence_relation(
207                EvidenceRelation::SupportedByInstrument,
208                Plane::Claim,
209                Plane::Instrument
210            ),
211            Ok(())
212        );
213        assert_eq!(
214            validate_evidence_relation(
215                EvidenceRelation::SupportedByInstrument,
216                Plane::Entity,
217                Plane::Instrument
218            ),
219            Err(QdnfError::Denied)
220        );
221        assert_eq!(
222            validate_evidence_relation(
223                EvidenceRelation::AssertedAbout,
224                Plane::Claim,
225                Plane::Entity
226            ),
227            Ok(())
228        );
229        assert_eq!(
230            validate_evidence_relation(
231                EvidenceRelation::ObservedAtHandle,
232                Plane::Claim,
233                Plane::Handle
234            ),
235            Ok(())
236        );
237        assert_eq!(
238            validate_evidence_relation(
239                EvidenceRelation::ObservedAtHandle,
240                Plane::Entity,
241                Plane::Handle
242            ),
243            Err(QdnfError::Denied)
244        );
245    }
246}