nizaam-core 1.0.1

Domain-agnostic Rust infrastructure core for building secure, observable, reliable, and extensible Nizaam engines.
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
//! Contract identification and compatibility helpers for the Control Plane.
//!
//! The existing `contracts` subsystem remains authoritative over the contract
//! model and compatibility algorithm. This module provides the Control Plane
//! boundary for identifying a request's contract and filtering offered
//! contracts without redefining those semantics.
//!
//! ```text
//! Universal Request / Contract Metadata
//!              │
//!              ▼
//!       Control Plane contract.rs
//!              │
//!       ┌──────┴──────┐
//!       ▼             ▼
//!   identify      compatibility
//!                     │
//!                     ▼
//!             compatible candidates
//! ```
//!
//! This module does not own payload decoding, domain semantics, capability
//! execution, provider resolution, destination selection, or routing policy.

use crate::contracts::compatibility::compare_contracts;
use crate::contracts::{ContractDescriptor, ContractMetadata, UniversalRequest, Version};
use crate::identity::{CapabilityId, ContractId};
use crate::status::Compatibility;
use std::fmt;

/// Identifies the contract descriptor carried by Control Plane contract
/// metadata.
///
/// The descriptor is returned by reference and is not reconstructed or
/// rewritten. Contract metadata remains owned by the existing `contracts`
/// subsystem.
#[must_use]
pub fn identify(metadata: &ContractMetadata) -> &ContractDescriptor {
    &metadata.descriptor
}

/// Identifies the contract descriptor carried by a universal request.
///
/// Structural request validation remains the responsibility of the existing
/// contracts validation subsystem. This helper only projects the already
/// declared contract descriptor.
#[must_use]
pub fn identify_request(request: &UniversalRequest) -> &ContractDescriptor {
    &request.event.envelope.metadata.descriptor
}

/// Compares a required contract with one offered contract using the canonical
/// compatibility implementation from the existing Contracts subsystem.
#[must_use]
pub fn compare(required: &ContractDescriptor, offered: &ContractDescriptor) -> Compatibility {
    compare_contracts(required, offered)
}

/// Returns only the offered contract descriptors that are established as
/// compatible with the required descriptor.
///
/// The input order is preserved. This function filters candidates; it does not
/// select a preferred version or provider. Final selection remains the
/// responsibility of later Control Plane resolution and policy composition.
#[must_use]
pub fn filter_compatible<'a>(
    required: &ContractDescriptor,
    offered: &'a [ContractDescriptor],
) -> Vec<&'a ContractDescriptor> {
    offered
        .iter()
        .filter(|candidate| compare(required, candidate) == Compatibility::Compatible)
        .collect()
}

/// Ensures that one offered contract is compatible with the required contract.
///
/// This is useful when an outer resolver has already selected one candidate and
/// wants the Control Plane contract boundary to establish compatibility without
/// making another selection decision.
///
/// [`Compatibility::Unknown`] is preserved as an explicit failure rather than
/// being silently treated as compatible.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum ContractResolutionError {
    /// The offered contract does not satisfy the required contract boundary.
    NotCompatible {
        contract_id: ContractId,
        capability_id: CapabilityId,
        required_version: Version,
        offered_version: Version,
        compatibility: Compatibility,
    },
}

impl fmt::Display for ContractResolutionError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::NotCompatible {
                contract_id,
                capability_id,
                required_version,
                offered_version,
                compatibility,
            } => write!(
                formatter,
                "contract {contract_id} for capability {capability_id} is not compatible: \
                 required version {required_version}, offered version {offered_version}, \
                 result {compatibility:?}"
            ),
        }
    }
}

impl std::error::Error for ContractResolutionError {}

/// Result alias for Control Plane contract compatibility operations.
pub type ContractResult<T> = Result<T, ContractResolutionError>;

/// Establishes that one offered contract is compatible with the required
/// contract.
pub fn require_compatible(
    required: &ContractDescriptor,
    offered: &ContractDescriptor,
) -> ContractResult<()> {
    let compatibility = compare(required, offered);

    if compatibility == Compatibility::Compatible {
        Ok(())
    } else {
        Err(ContractResolutionError::NotCompatible {
            contract_id: required.contract_id.clone(),
            capability_id: required.capability_id.clone(),
            required_version: required.version.clone(),
            offered_version: offered.version.clone(),
            compatibility,
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::contracts::{
        ContractDescriptor, Interaction, PayloadDescriptor, RequirementsMetadata,
    };
    use crate::contracts::{EncodedPayload, MessageEnvelope, Participants};
    use crate::identity::{CapabilityId, ContractId, EngineId};
    use crate::identity::{CorrelationId, MessageId, OperationId};
    use crate::operation::{Operation, OperationContext};

    fn descriptor(
        contract_id: &str,
        capability_id: &str,
        version: Version,
        interaction: Interaction,
        media_type: &str,
        schema_version: Version,
    ) -> ContractDescriptor {
        ContractDescriptor::new(
            ContractId::new(contract_id).expect("valid contract id"),
            CapabilityId::new(capability_id).expect("valid capability id"),
            version,
            interaction,
            PayloadDescriptor::new(media_type, schema_version).expect("valid payload descriptor"),
        )
    }

    fn metadata(descriptor: ContractDescriptor) -> ContractMetadata {
        ContractMetadata::new(
            descriptor,
            Participants::new(
                EngineId::new("caller").expect("valid sender"),
                EngineId::new("provider").expect("valid target"),
            ),
        )
    }

    fn request(descriptor: ContractDescriptor) -> UniversalRequest {
        let payload = EncodedPayload::new(descriptor.payload.clone(), b"payload".to_vec());
        let metadata = metadata(descriptor);
        let operation = OperationContext::new(Operation::new(
            OperationId::new("operation-1").expect("valid operation id"),
            CorrelationId::new("correlation-1").expect("valid correlation id"),
        ));

        UniversalRequest::new(MessageEnvelope::new(
            MessageId::new("message-1").expect("valid message id"),
            operation,
            metadata,
            payload,
        ))
    }

    #[test]
    fn identify_returns_the_declared_contract_descriptor() {
        let descriptor = descriptor(
            "quran.retrieve",
            "quran.retrieve",
            Version::new(2, 0, 0),
            Interaction::Request,
            "application/json",
            Version::new(1, 0, 0),
        );
        let metadata = metadata(descriptor.clone());

        let identified = identify(&metadata);

        assert_eq!(identified, &descriptor);
        assert_eq!(identified.contract_id.as_str(), "quran.retrieve");
    }

    #[test]
    fn identify_request_returns_the_request_contract_descriptor() {
        let descriptor = descriptor(
            "lookup.request",
            "lookup",
            Version::new(1, 0, 0),
            Interaction::Request,
            "application/json",
            Version::new(1, 0, 0),
        );
        let request = request(descriptor.clone());

        assert_eq!(identify_request(&request), &descriptor);
    }

    #[test]
    fn compare_delegates_to_canonical_contract_compatibility() {
        let required = descriptor(
            "lookup",
            "lookup",
            Version::new(1, 0, 0),
            Interaction::Request,
            "application/json",
            Version::new(1, 0, 0),
        );
        let offered = descriptor(
            "lookup",
            "lookup",
            Version::new(1, 2, 0),
            Interaction::Request,
            "application/json",
            Version::new(1, 1, 0),
        );

        assert_eq!(compare(&required, &offered), Compatibility::Compatible);
    }

    #[test]
    fn incompatible_contract_identity_is_not_accepted() {
        let required = descriptor(
            "lookup",
            "lookup",
            Version::new(1, 0, 0),
            Interaction::Request,
            "application/json",
            Version::new(1, 0, 0),
        );
        let offered = descriptor(
            "other.contract",
            "lookup",
            Version::new(1, 0, 0),
            Interaction::Request,
            "application/json",
            Version::new(1, 0, 0),
        );

        assert_eq!(compare(&required, &offered), Compatibility::Incompatible);
    }

    #[test]
    fn incompatible_capability_is_not_accepted() {
        let required = descriptor(
            "lookup",
            "lookup",
            Version::new(1, 0, 0),
            Interaction::Request,
            "application/json",
            Version::new(1, 0, 0),
        );
        let offered = descriptor(
            "lookup",
            "search",
            Version::new(1, 0, 0),
            Interaction::Request,
            "application/json",
            Version::new(1, 0, 0),
        );

        assert_eq!(compare(&required, &offered), Compatibility::Incompatible);
    }

    #[test]
    fn incompatible_interaction_is_not_accepted() {
        let required = descriptor(
            "lookup",
            "lookup",
            Version::new(1, 0, 0),
            Interaction::Request,
            "application/json",
            Version::new(1, 0, 0),
        );
        let offered = descriptor(
            "lookup",
            "lookup",
            Version::new(1, 0, 0),
            Interaction::Response,
            "application/json",
            Version::new(1, 0, 0),
        );

        assert_eq!(compare(&required, &offered), Compatibility::Incompatible);
    }

    #[test]
    fn incompatible_media_type_is_not_accepted() {
        let required = descriptor(
            "lookup",
            "lookup",
            Version::new(1, 0, 0),
            Interaction::Request,
            "application/json",
            Version::new(1, 0, 0),
        );
        let offered = descriptor(
            "lookup",
            "lookup",
            Version::new(1, 0, 0),
            Interaction::Request,
            "application/problem+json",
            Version::new(1, 0, 0),
        );

        assert_eq!(compare(&required, &offered), Compatibility::Incompatible);
    }

    #[test]
    fn unknown_version_result_is_preserved() {
        let required = descriptor(
            "lookup",
            "lookup",
            Version::new(1, 5, 0),
            Interaction::Request,
            "application/json",
            Version::new(1, 0, 0),
        );
        let offered = descriptor(
            "lookup",
            "lookup",
            Version::new(1, 2, 0),
            Interaction::Request,
            "application/json",
            Version::new(1, 0, 0),
        );

        assert_eq!(compare(&required, &offered), Compatibility::Unknown);
    }

    #[test]
    fn incompatible_schema_major_version_is_rejected() {
        let required = descriptor(
            "lookup",
            "lookup",
            Version::new(1, 0, 0),
            Interaction::Request,
            "application/json",
            Version::new(1, 0, 0),
        );
        let offered = descriptor(
            "lookup",
            "lookup",
            Version::new(1, 0, 0),
            Interaction::Request,
            "application/json",
            Version::new(2, 0, 0),
        );

        assert_eq!(compare(&required, &offered), Compatibility::Incompatible);
    }

    #[test]
    fn filter_compatible_preserves_input_order() {
        let required = descriptor(
            "lookup",
            "lookup",
            Version::new(1, 0, 0),
            Interaction::Request,
            "application/json",
            Version::new(1, 0, 0),
        );
        let offered_a = descriptor(
            "lookup",
            "lookup",
            Version::new(1, 0, 0),
            Interaction::Request,
            "application/json",
            Version::new(1, 0, 0),
        );
        let offered_b = descriptor(
            "lookup",
            "lookup",
            Version::new(2, 0, 0),
            Interaction::Request,
            "application/json",
            Version::new(1, 0, 0),
        );
        let offered_c = descriptor(
            "lookup",
            "lookup",
            Version::new(1, 1, 0),
            Interaction::Request,
            "application/json",
            Version::new(1, 0, 0),
        );
        let candidates = vec![offered_a.clone(), offered_b, offered_c.clone()];

        let compatible = filter_compatible(&required, &candidates);

        assert_eq!(compatible, vec![&offered_a, &offered_c]);
    }

    #[test]
    fn filter_compatible_excludes_unknown_results() {
        let required = descriptor(
            "lookup",
            "lookup",
            Version::new(1, 5, 0),
            Interaction::Request,
            "application/json",
            Version::new(1, 0, 0),
        );
        let unknown = descriptor(
            "lookup",
            "lookup",
            Version::new(1, 2, 0),
            Interaction::Request,
            "application/json",
            Version::new(1, 0, 0),
        );
        let compatible = descriptor(
            "lookup",
            "lookup",
            Version::new(1, 6, 0),
            Interaction::Request,
            "application/json",
            Version::new(1, 0, 0),
        );
        let candidates = vec![unknown, compatible.clone()];

        let result = filter_compatible(&required, &candidates);

        assert_eq!(result, vec![&compatible]);
    }

    #[test]
    fn filter_compatible_returns_empty_when_no_candidate_matches() {
        let required = descriptor(
            "lookup",
            "lookup",
            Version::new(2, 0, 0),
            Interaction::Request,
            "application/json",
            Version::new(1, 0, 0),
        );
        let candidates = vec![descriptor(
            "lookup",
            "lookup",
            Version::new(1, 0, 0),
            Interaction::Request,
            "application/json",
            Version::new(1, 0, 0),
        )];

        assert!(filter_compatible(&required, &candidates).is_empty());
    }

    #[test]
    fn require_compatible_accepts_a_compatible_offer() {
        let required = descriptor(
            "lookup",
            "lookup",
            Version::new(1, 0, 0),
            Interaction::Request,
            "application/json",
            Version::new(1, 0, 0),
        );
        let offered = descriptor(
            "lookup",
            "lookup",
            Version::new(1, 1, 0),
            Interaction::Request,
            "application/json",
            Version::new(1, 1, 0),
        );

        assert_eq!(require_compatible(&required, &offered), Ok(()));
    }

    #[test]
    fn require_compatible_preserves_unknown_failure() {
        let required = descriptor(
            "lookup",
            "lookup",
            Version::new(1, 5, 0),
            Interaction::Request,
            "application/json",
            Version::new(1, 0, 0),
        );
        let offered = descriptor(
            "lookup",
            "lookup",
            Version::new(1, 2, 0),
            Interaction::Request,
            "application/json",
            Version::new(1, 0, 0),
        );

        assert_eq!(
            require_compatible(&required, &offered),
            Err(ContractResolutionError::NotCompatible {
                contract_id: ContractId::new("lookup").unwrap(),
                capability_id: CapabilityId::new("lookup").unwrap(),
                required_version: Version::new(1, 5, 0),
                offered_version: Version::new(1, 2, 0),
                compatibility: Compatibility::Unknown,
            })
        );
    }

    #[test]
    fn requirement_metadata_is_not_reinterpreted_by_contract_comparison() {
        let descriptor = descriptor(
            "lookup",
            "lookup",
            Version::new(1, 0, 0),
            Interaction::Request,
            "application/json",
            Version::new(1, 0, 0),
        );
        let metadata = ContractMetadata::new(
            descriptor.clone(),
            Participants::new(
                EngineId::new("caller").unwrap(),
                EngineId::new("provider").unwrap(),
            ),
        )
        .with_requirements(
            RequirementsMetadata::none()
                .requiring_capability(CapabilityId::new("lookup").unwrap())
                .requiring_contract_version(Version::new(1, 0, 0)),
        );

        assert_eq!(identify(&metadata), &descriptor);
    }
}