type-bridge-contract 2.1.0

Versioned binding-neutral contract primitives for type-bridge
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
//! Trusted, reversible schema transitions over one durable managed scope.

use std::collections::BTreeSet;

use serde::Serialize;

use crate::capability::{CapabilityId, CapabilitySet};
use crate::codec::{FormatVersion, ensure_format_version, to_canonical_json};
use crate::diagnostic::{Diagnostic, DiagnosticCategory};
use crate::limits::MAX_CANONICAL_COLLECTION_LEN;
use crate::managed_scope::ManagedScopeBinding;
use crate::schema::{DeclaredIdentityFingerprint, SchemaFact, SchemaFactId};
use crate::schema_fingerprint::{
    ManagedDeclaredIdentityFingerprint, ManagedSemanticSchemaFingerprint,
};

/// Transition capability required only when a patch uses provider-native redefinition.
pub const SCHEMA_REDEFINE_CAPABILITY: &str = "schema.redefine";

/// Closed capability IDs required by the retained schema-transition profile.
pub const SCHEMA_TRANSITION_CAPABILITY_IDS: &[&str] = &[
    "schema.transaction.atomic",
    "schema.transition.define",
    "schema.transition.undefine",
    "schema.transition.redefine.sub",
    "schema.transition.redefine.value",
    "schema.transition.redefine.relates.specialization",
    "schema.transition.redefine.annotation",
    "schema.transition.redefine.function",
    "schema.transition.replace.sub.annotation",
];

/// Return the closed capability vocabulary for schema-transition execution.
#[must_use]
pub fn schema_transition_capability_vocabulary() -> CapabilitySet {
    SCHEMA_TRANSITION_CAPABILITY_IDS
        .iter()
        .map(|capability| {
            CapabilityId::new(*capability)
                .expect("static schema-transition capability ID is canonical")
        })
        .collect()
}

/// The format version of a canonical schema patch.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize)]
#[serde(transparent)]
pub struct PatchFormatVersion(u16);

impl PatchFormatVersion {
    /// The first canonical schema-patch format.
    pub const V1: Self = Self(1);

    /// Return the raw version number.
    pub const fn get(self) -> u16 {
        self.0
    }

    pub(crate) fn from_wire(value: u16) -> Result<Self, Diagnostic> {
        if value == Self::V1.get() {
            Ok(Self::V1)
        } else {
            Err(delta_diagnostic(
                DiagnosticCategory::InvalidContract,
                "unsupported_patch_format_version",
                "schema patch format version is not supported",
            )
            .with_detail("actual", i64::from(value))
            .with_detail("supported", i64::from(Self::V1.get())))
        }
    }
}

/// Deterministically ordered identities owned by one managed schema scope.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[serde(transparent)]
pub struct ManagedFactSelection(BTreeSet<SchemaFactId>);

impl ManagedFactSelection {
    /// Build a bounded selection, rejecting duplicate fact identities.
    pub fn new(fact_ids: impl IntoIterator<Item = SchemaFactId>) -> Result<Self, Diagnostic> {
        let mut selection = BTreeSet::new();
        for fact_id in fact_ids {
            if !selection.insert(fact_id) {
                return Err(delta_diagnostic(
                    DiagnosticCategory::InvalidContract,
                    "duplicate_managed_fact_id",
                    "managed fact selection contains a duplicate identity",
                ));
            }
            if selection.len() > MAX_CANONICAL_COLLECTION_LEN {
                return Err(delta_diagnostic(
                    DiagnosticCategory::ResourceLimit,
                    "too_many_managed_fact_ids",
                    "managed fact selection exceeds the canonical collection limit",
                ));
            }
        }
        Ok(Self(selection))
    }

    /// Return an empty managed selection.
    #[must_use]
    pub fn empty() -> Self {
        Self(BTreeSet::new())
    }

    /// Return whether this selection contains one fact identity.
    pub fn contains(&self, fact_id: &SchemaFactId) -> bool {
        self.0.contains(fact_id)
    }

    /// Return the number of selected fact identities.
    pub fn len(&self) -> usize {
        self.0.len()
    }

    /// Return whether no facts are selected.
    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }

    /// Iterate in stable fact-identity order.
    pub fn iter(&self) -> impl ExactSizeIterator<Item = &SchemaFactId> {
        self.0.iter()
    }
}

/// Fingerprint-bound state of one explicitly selected managed schema scope.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct ManagedSchemaState {
    declared_identity: DeclaredIdentityFingerprint,
    format: FormatVersion,
    managed_declared_identity: ManagedDeclaredIdentityFingerprint,
    managed_semantic_schema: ManagedSemanticSchemaFingerprint,
    required_capabilities: CapabilitySet,
    scope: ManagedScopeBinding,
    selection: ManagedFactSelection,
}

impl ManagedSchemaState {
    /// Bind one exact declaration, selection, and its managed fingerprint views.
    pub fn new(
        format: FormatVersion,
        required_capabilities: CapabilitySet,
        scope: ManagedScopeBinding,
        selection: ManagedFactSelection,
        declared_identity: DeclaredIdentityFingerprint,
        managed_declared_identity: ManagedDeclaredIdentityFingerprint,
        managed_semantic_schema: ManagedSemanticSchemaFingerprint,
    ) -> Result<Self, Diagnostic> {
        ensure_format_version(format, FormatVersion::V1)?;
        Ok(Self {
            declared_identity,
            format,
            managed_declared_identity,
            managed_semantic_schema,
            required_capabilities,
            scope,
            selection,
        })
    }

    /// Return the owning schema format.
    pub const fn format(&self) -> FormatVersion {
        self.format
    }

    /// Return capabilities required by the selected schema state.
    pub const fn required_capabilities(&self) -> &CapabilitySet {
        &self.required_capabilities
    }

    /// Return the durable managed-scope binding.
    pub const fn scope(&self) -> &ManagedScopeBinding {
        &self.scope
    }

    /// Return the exact selected fact identities.
    pub const fn selection(&self) -> &ManagedFactSelection {
        &self.selection
    }

    /// Return the full declared-schema identity used by resolution.
    pub const fn declared_identity(&self) -> &DeclaredIdentityFingerprint {
        &self.declared_identity
    }

    /// Return the managed declared-identity fingerprint.
    pub const fn managed_declared_identity(&self) -> &ManagedDeclaredIdentityFingerprint {
        &self.managed_declared_identity
    }

    /// Return the managed semantic-schema fingerprint.
    pub const fn managed_semantic_schema(&self) -> &ManagedSemanticSchemaFingerprint {
        &self.managed_semantic_schema
    }
}

/// Public discriminator for an opaque schema operation.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SchemaOperationKind {
    /// Define one non-empty, identity-sorted fact group.
    Define,
    /// Replace one fact with an unequal payload under the same identity.
    Redefine,
    /// Remove one exact fact.
    Undefine,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
enum SchemaOperationData {
    Define {
        facts: Vec<SchemaFact>,
    },
    Redefine {
        expected: Box<SchemaFact>,
        replacement: Box<SchemaFact>,
    },
    Undefine {
        fact: SchemaFact,
    },
}

/// One validated schema transition with an exact offline inverse.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[serde(transparent)]
pub struct SchemaOperation(SchemaOperationData);

impl SchemaOperation {
    /// Define a non-empty fact group, canonicalized into fact-identity order.
    pub fn define(mut facts: Vec<SchemaFact>) -> Result<Self, Diagnostic> {
        if facts.is_empty() {
            return Err(delta_diagnostic(
                DiagnosticCategory::InvalidContract,
                "empty_schema_define",
                "schema define operation requires at least one fact",
            ));
        }
        if facts.len() > MAX_CANONICAL_COLLECTION_LEN {
            return Err(delta_diagnostic(
                DiagnosticCategory::ResourceLimit,
                "too_many_schema_define_facts",
                "schema define operation exceeds the canonical collection limit",
            ));
        }
        facts.sort_by_key(SchemaFact::id);
        if facts.windows(2).any(|pair| pair[0].id() == pair[1].id()) {
            return Err(delta_diagnostic(
                DiagnosticCategory::InvalidContract,
                "duplicate_schema_operation_fact_id",
                "schema define operation contains duplicate fact identities",
            ));
        }
        Ok(Self(SchemaOperationData::Define { facts }))
    }

    /// Redefine an existing fact without changing its stable identity.
    pub fn redefine(expected: SchemaFact, replacement: SchemaFact) -> Result<Self, Diagnostic> {
        if expected.id() != replacement.id() {
            return Err(delta_diagnostic(
                DiagnosticCategory::InvalidContract,
                "schema_redefine_identity_mismatch",
                "schema redefinition must preserve the fact identity",
            ));
        }
        if expected == replacement {
            return Err(delta_diagnostic(
                DiagnosticCategory::InvalidContract,
                "schema_redefine_noop",
                "schema redefinition requires an unequal replacement payload",
            ));
        }
        Ok(Self(SchemaOperationData::Redefine {
            expected: Box::new(expected),
            replacement: Box::new(replacement),
        }))
    }

    /// Undefine one exact fact.
    #[must_use]
    pub const fn undefine(fact: SchemaFact) -> Self {
        Self(SchemaOperationData::Undefine { fact })
    }

    /// Return the operation discriminator.
    #[must_use]
    pub const fn kind(&self) -> SchemaOperationKind {
        match &self.0 {
            SchemaOperationData::Define { .. } => SchemaOperationKind::Define,
            SchemaOperationData::Redefine { .. } => SchemaOperationKind::Redefine,
            SchemaOperationData::Undefine { .. } => SchemaOperationKind::Undefine,
        }
    }

    /// Return defined facts when this is a define operation.
    pub fn defined_facts(&self) -> Option<&[SchemaFact]> {
        match &self.0 {
            SchemaOperationData::Define { facts } => Some(facts),
            _ => None,
        }
    }

    /// Return the expected current fact when this is a redefinition.
    pub fn expected_fact(&self) -> Option<&SchemaFact> {
        match &self.0 {
            SchemaOperationData::Redefine { expected, .. } => Some(expected),
            _ => None,
        }
    }

    /// Return the replacement fact when this is a redefinition.
    pub fn replacement_fact(&self) -> Option<&SchemaFact> {
        match &self.0 {
            SchemaOperationData::Redefine { replacement, .. } => Some(replacement),
            _ => None,
        }
    }

    /// Return the exact removed fact when this is an undefinition.
    pub const fn undefined_fact(&self) -> Option<&SchemaFact> {
        match &self.0 {
            SchemaOperationData::Undefine { fact } => Some(fact),
            _ => None,
        }
    }

    /// Return all affected identities in canonical order for this operation.
    #[must_use]
    pub fn affected_ids(&self) -> Vec<SchemaFactId> {
        match &self.0 {
            SchemaOperationData::Define { facts } => facts.iter().map(SchemaFact::id).collect(),
            SchemaOperationData::Redefine { expected, .. } => vec![expected.id()],
            SchemaOperationData::Undefine { fact } => vec![fact.id()],
        }
    }

    /// Derive exact inverse operations; grouped definitions invert in reverse order.
    #[must_use]
    pub fn inverse(&self) -> Vec<Self> {
        match &self.0 {
            SchemaOperationData::Define { facts } => {
                facts.iter().rev().cloned().map(Self::undefine).collect()
            }
            SchemaOperationData::Redefine {
                expected,
                replacement,
            } => vec![
                Self::redefine(replacement.as_ref().clone(), expected.as_ref().clone())
                    .expect("an inverse redefinition preserves a validated unequal identity"),
            ],
            SchemaOperationData::Undefine { fact } => vec![
                Self::define(vec![fact.clone()])
                    .expect("a singleton inverse definition is always non-empty and unique"),
            ],
        }
    }
}

/// One immutable ordered patch between two exact managed schema states.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct SchemaDelta {
    format: PatchFormatVersion,
    operations: Vec<SchemaOperation>,
    required_capabilities: CapabilitySet,
    source: ManagedSchemaState,
    target: ManagedSchemaState,
}

impl SchemaDelta {
    /// Validate a complete managed-state transition and derive its capabilities.
    pub fn new(
        format: PatchFormatVersion,
        source: ManagedSchemaState,
        target: ManagedSchemaState,
        operations: Vec<SchemaOperation>,
    ) -> Result<Self, Diagnostic> {
        PatchFormatVersion::from_wire(format.get())?;
        if source.format() != target.format() {
            return Err(delta_diagnostic(
                DiagnosticCategory::InvalidContract,
                "schema_delta_format_transition",
                "schema delta source and target formats must match exactly",
            ));
        }
        if source.scope() != target.scope() {
            return Err(delta_diagnostic(
                DiagnosticCategory::InvalidContract,
                "schema_delta_scope_transition",
                "schema delta cannot change its durable managed-scope binding",
            ));
        }
        if source
            .managed_semantic_schema()
            .as_fingerprint()
            .semantic_profile()
            != target
                .managed_semantic_schema()
                .as_fingerprint()
                .semantic_profile()
        {
            return Err(delta_diagnostic(
                DiagnosticCategory::InvalidContract,
                "schema_delta_semantic_profile_transition",
                "schema delta cannot cross semantic-profile identities",
            ));
        }
        if source == target {
            return Err(delta_diagnostic(
                DiagnosticCategory::InvalidContract,
                "schema_delta_noop",
                "schema delta source and target states must differ",
            ));
        }
        if operations.is_empty()
            && (source.selection() != target.selection()
                || source.required_capabilities() == target.required_capabilities()
                || source.managed_declared_identity() == target.managed_declared_identity()
                || source.managed_semantic_schema() == target.managed_semantic_schema())
        {
            return Err(delta_diagnostic(
                DiagnosticCategory::InvalidContract,
                "invalid_schema_delta_capability_transition",
                "operation-free schema deltas require an unchanged selection plus distinct capabilities and managed fingerprints",
            ));
        }
        if operations.len() > MAX_CANONICAL_COLLECTION_LEN {
            return Err(delta_diagnostic(
                DiagnosticCategory::ResourceLimit,
                "too_many_schema_operations",
                "schema delta exceeds the canonical operation limit",
            ));
        }
        let mut affected = BTreeSet::new();
        let mut transitioned = source.selection.0.clone();
        for operation in &operations {
            for fact_id in operation.affected_ids() {
                if !affected.insert(fact_id) {
                    return Err(delta_diagnostic(
                        DiagnosticCategory::InvalidContract,
                        "duplicate_schema_delta_fact_id",
                        "schema delta operations affect one fact identity more than once",
                    ));
                }
            }
            match &operation.0 {
                SchemaOperationData::Define { facts } => {
                    for fact in facts {
                        if !transitioned.insert(fact.id()) {
                            return Err(delta_diagnostic(
                                DiagnosticCategory::InvalidContract,
                                "schema_delta_define_existing_fact",
                                "schema delta defines an identity already present in its source selection",
                            ));
                        }
                    }
                }
                SchemaOperationData::Redefine { expected, .. } => {
                    if !transitioned.contains(&expected.id()) {
                        return Err(delta_diagnostic(
                            DiagnosticCategory::InvalidContract,
                            "schema_delta_redefine_missing_fact",
                            "schema delta redefines an identity absent from its source selection",
                        ));
                    }
                }
                SchemaOperationData::Undefine { fact } => {
                    if !transitioned.remove(&fact.id()) {
                        return Err(delta_diagnostic(
                            DiagnosticCategory::InvalidContract,
                            "schema_delta_undefine_missing_fact",
                            "schema delta undefines an identity absent from its source selection",
                        ));
                    }
                }
            }
        }
        if transitioned != target.selection.0 {
            return Err(delta_diagnostic(
                DiagnosticCategory::InvalidContract,
                "schema_delta_selection_mismatch",
                "schema operations do not produce the declared target managed selection",
            ));
        }

        let mut required_capabilities = source
            .required_capabilities()
            .iter()
            .chain(target.required_capabilities().iter())
            .cloned()
            .collect::<CapabilitySet>();
        if operations
            .iter()
            .any(|operation| operation.kind() == SchemaOperationKind::Redefine)
        {
            required_capabilities.insert(CapabilityId::new(SCHEMA_REDEFINE_CAPABILITY)?);
        }

        Ok(Self {
            format,
            operations,
            required_capabilities,
            source,
            target,
        })
    }

    /// Return the schema-patch format.
    pub const fn format(&self) -> PatchFormatVersion {
        self.format
    }

    /// Return the exact source-state precondition.
    pub const fn source(&self) -> &ManagedSchemaState {
        &self.source
    }

    /// Return the exact target state.
    pub const fn target(&self) -> &ManagedSchemaState {
        &self.target
    }

    /// Return operations in caller-preserved transition order.
    pub fn operations(&self) -> &[SchemaOperation] {
        &self.operations
    }

    /// Return capabilities derived from both states and the transition table.
    pub const fn required_capabilities(&self) -> &CapabilitySet {
        &self.required_capabilities
    }

    /// Encode this trusted delta as exact canonical JSON.
    pub fn canonical_bytes(&self) -> Result<Vec<u8>, Diagnostic> {
        encode_schema_delta(self)
    }
}

/// Encode only a previously validated trusted schema delta.
pub fn encode_schema_delta(delta: &SchemaDelta) -> Result<Vec<u8>, Diagnostic> {
    to_canonical_json(delta)
}

/// Decode canonical bytes through private wire DTOs and every trusted constructor.
pub fn decode_schema_delta(bytes: &[u8]) -> Result<SchemaDelta, Diagnostic> {
    crate::schema_delta_wire::decode_schema_delta(bytes)
}

fn delta_diagnostic(
    category: DiagnosticCategory,
    code: &'static str,
    message: &'static str,
) -> Diagnostic {
    Diagnostic::stable(category, code, message)
}