type-bridge-schema 2.1.0

Canonical schema resolution and projection engine 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
//! Constructor-verified, source-free schema authority artifacts.

use std::error::Error;
use std::fmt;

use serde::{Deserialize, Serialize};
use serde_json::Value;
use type_bridge_contract::capability::{CapabilityId, CapabilitySet};
use type_bridge_contract::codec::{
    CodecVersion, FormatVersion, from_canonical_json, to_canonical_json,
};
use type_bridge_contract::diagnostic::{Diagnostic, DiagnosticCategory};
use type_bridge_contract::fingerprint::{
    CanonicalizationVersion, Fingerprint, FingerprintDomain, SemanticProfileId,
};
use type_bridge_contract::limits::MAX_CANONICAL_BYTES;
use type_bridge_contract::managed_scope::{
    ManagedScopeBinding, ManagedScopeId, ManagedScopeProfileId, SemanticProfileBinding,
};
use type_bridge_contract::migration::CONDITIONAL_RESOLUTION_CAPABILITY;
use type_bridge_contract::migration_assertion_capability_vocabulary;
use type_bridge_contract::query_plan::query_plan_v2_capability_vocabulary;
use type_bridge_contract::schema::{
    DeclaredSchema, ManagedSchemaState, SchemaDiagnostics, decode_declared_schema,
    encode_declared_schema,
};
use type_bridge_contract::schema_delta::{
    SCHEMA_REDEFINE_CAPABILITY, schema_transition_capability_vocabulary,
};

use crate::{
    BUILTIN_SCHEMA_CAPABILITY_IDS, DeltaError, ManagedDeltaContext, ResolvedSchema,
    managed_schema_state, resolve_schema_with_capabilities,
};

/// The first source-free compiled schema-authority envelope.
pub const TYPEBRIDGE_SCHEMA_AUTHORITY_V1: &str = "typebridge.schema-authority/v1";
/// Fingerprint domain for one exact canonical schema-authority content envelope.
pub const SCHEMA_AUTHORITY_FINGERPRINT_DOMAIN: &str = "typebridge.schema.authority";
/// Canonicalization identity for the first schema-authority content envelope.
pub const SCHEMA_AUTHORITY_FINGERPRINT_CANONICALIZATION: &str = "typebridge.schema-authority/v1";
/// Maximum canonical schema-authority size under the shared contract codec.
pub const MAX_SCHEMA_AUTHORITY_BYTES: usize = MAX_CANONICAL_BYTES;

/// Return every capability understood by generated packages and the generic server.
///
/// The authority may bind query, schema-resolution, and workspace migration
/// requirements. Consumers use this closed vocabulary only to reconstruct and
/// validate that envelope; execution surfaces still advertise their narrower
/// operation-specific capabilities.
#[must_use]
pub fn schema_authority_capability_vocabulary() -> CapabilitySet {
    let mut capabilities = query_plan_v2_capability_vocabulary();
    for capability in schema_transition_capability_vocabulary()
        .into_iter()
        .chain(migration_assertion_capability_vocabulary())
    {
        capabilities.insert(capability);
    }
    for capability in BUILTIN_SCHEMA_CAPABILITY_IDS {
        capabilities.insert(
            CapabilityId::new(*capability).expect("built-in schema capability ID is canonical"),
        );
    }
    for capability in [
        SCHEMA_REDEFINE_CAPABILITY,
        CONDITIONAL_RESOLUTION_CAPABILITY,
    ] {
        capabilities.insert(
            CapabilityId::new(capability).expect("static authority capability ID is canonical"),
        );
    }
    capabilities
}

/// Stable high-level classification for schema-authority failures.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum SchemaAuthorityErrorCode {
    /// Canonical bytes or a nested contract value are malformed.
    Contract,
    /// Declared-schema construction or semantic resolution failed.
    Schema,
    /// The authority, codec, or schema-IR version is unsupported.
    UnsupportedVersion,
    /// A required capability is absent from the available capability set.
    UnsupportedCapability,
    /// A canonical structural ceiling was exceeded.
    ResourceLimit,
    /// A bound fingerprint, scope, profile, or state is stale.
    IntegrityMismatch,
}

/// A fail-closed schema-authority failure retaining nested diagnostics.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct SchemaAuthorityError {
    code: SchemaAuthorityErrorCode,
    message: &'static str,
    contract: Option<Box<Diagnostic>>,
    schema: Option<Box<SchemaDiagnostics>>,
}

impl SchemaAuthorityError {
    fn new(code: SchemaAuthorityErrorCode, message: &'static str) -> Self {
        Self {
            code,
            message,
            contract: None,
            schema: None,
        }
    }

    /// Return the stable high-level failure classification.
    #[must_use]
    pub const fn code(&self) -> SchemaAuthorityErrorCode {
        self.code
    }

    /// Return the nested contract diagnostic, when one caused the failure.
    #[must_use]
    pub fn contract(&self) -> Option<&Diagnostic> {
        self.contract.as_deref()
    }

    /// Return nested schema diagnostics, when reconstruction or resolution failed.
    #[must_use]
    pub fn schema(&self) -> Option<&SchemaDiagnostics> {
        self.schema.as_deref()
    }
}

impl fmt::Display for SchemaAuthorityError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str(self.message)
    }
}

impl Error for SchemaAuthorityError {}

impl From<Diagnostic> for SchemaAuthorityError {
    fn from(value: Diagnostic) -> Self {
        let code = match value.category() {
            DiagnosticCategory::InvalidContract => SchemaAuthorityErrorCode::Contract,
            DiagnosticCategory::UnsupportedCapability => {
                SchemaAuthorityErrorCode::UnsupportedCapability
            }
            DiagnosticCategory::ResourceLimit => SchemaAuthorityErrorCode::ResourceLimit,
            DiagnosticCategory::Integrity => SchemaAuthorityErrorCode::IntegrityMismatch,
        };
        Self {
            code,
            message: "a schema-authority contract is invalid",
            contract: Some(Box::new(value)),
            schema: None,
        }
    }
}

impl From<SchemaDiagnostics> for SchemaAuthorityError {
    fn from(value: SchemaDiagnostics) -> Self {
        Self {
            code: SchemaAuthorityErrorCode::Schema,
            message: "schema-authority reconstruction failed",
            contract: None,
            schema: Some(Box::new(value)),
        }
    }
}

impl From<DeltaError> for SchemaAuthorityError {
    fn from(value: DeltaError) -> Self {
        match value {
            DeltaError::Contract(error) => error.into(),
            DeltaError::Schema(error) => error.into(),
        }
    }
}

#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
struct SemanticProfileBindingWire {
    fingerprint: Value,
    id: SemanticProfileId,
}

impl SemanticProfileBindingWire {
    fn from_binding(binding: &SemanticProfileBinding) -> Result<Self, SchemaAuthorityError> {
        Ok(Self {
            fingerprint: canonical_value(binding.fingerprint().as_fingerprint())?,
            id: binding.id().clone(),
        })
    }

    fn rebuild(self) -> Result<SemanticProfileBinding, SchemaAuthorityError> {
        let trusted = SemanticProfileBinding::resolve(self.id.clone())?;
        require_exact_value(
            &self.fingerprint,
            trusted.fingerprint().as_fingerprint(),
            "semantic-profile fingerprint is stale",
        )?;
        Ok(trusted)
    }
}

#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
struct ManagedScopeProfileBindingWire {
    fingerprint: Value,
    id: ManagedScopeProfileId,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
struct ManagedScopeBindingWire {
    id: ManagedScopeId,
    profile: ManagedScopeProfileBindingWire,
}

impl ManagedScopeBindingWire {
    fn from_binding(binding: &ManagedScopeBinding) -> Result<Self, SchemaAuthorityError> {
        Ok(Self {
            id: binding.id().clone(),
            profile: ManagedScopeProfileBindingWire {
                fingerprint: canonical_value(binding.profile().fingerprint().as_fingerprint())?,
                id: binding.profile().id().clone(),
            },
        })
    }

    fn rebuild(self) -> Result<ManagedScopeBinding, SchemaAuthorityError> {
        let trusted = ManagedScopeBinding::exclusive(self.id.clone())?;
        if self.profile.id != *trusted.profile().id() {
            return Err(integrity_failure("managed-scope profile identity is stale"));
        }
        require_exact_value(
            &self.profile.fingerprint,
            trusted.profile().fingerprint().as_fingerprint(),
            "managed-scope profile fingerprint is stale",
        )?;
        Ok(trusted)
    }
}

#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
struct SchemaAuthorityContentWire {
    authority_version: String,
    codec_version: u16,
    declared_identity: Value,
    declared_schema: Value,
    managed_scope: ManagedScopeBindingWire,
    managed_state: Value,
    required_capabilities: CapabilitySet,
    schema_ir_version: u16,
    semantic_profile: SemanticProfileBindingWire,
    semantic_schema: Value,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
struct SchemaAuthorityWire {
    authority_fingerprint: Value,
    content: SchemaAuthorityContentWire,
}

/// Opaque source-free authority accepted only after complete reconstruction.
#[derive(Clone, Debug)]
pub struct VerifiedSchemaAuthority {
    authority_fingerprint: Fingerprint,
    canonical_bytes: Vec<u8>,
    declared: DeclaredSchema,
    managed_scope: ManagedScopeBinding,
    managed_state: ManagedSchemaState,
    required_capabilities: CapabilitySet,
    resolved: ResolvedSchema,
    semantic_profile: SemanticProfileBinding,
}

impl VerifiedSchemaAuthority {
    /// Return the exact fingerprint of the authority content envelope.
    #[must_use]
    pub const fn authority_fingerprint(&self) -> &Fingerprint {
        &self.authority_fingerprint
    }

    /// Return the constructor-validated direct declaration.
    #[must_use]
    pub const fn declared_schema(&self) -> &DeclaredSchema {
        &self.declared
    }

    /// Return independently reconstructed effective schema semantics.
    #[must_use]
    pub const fn resolved_schema(&self) -> &ResolvedSchema {
        &self.resolved
    }

    /// Return the registry-resolved semantic-profile identity and content binding.
    #[must_use]
    pub const fn semantic_profile(&self) -> &SemanticProfileBinding {
        &self.semantic_profile
    }

    /// Return the registry-resolved durable managed-scope binding.
    #[must_use]
    pub const fn managed_scope(&self) -> &ManagedScopeBinding {
        &self.managed_scope
    }

    /// Return the independently reconstructed managed schema state.
    #[must_use]
    pub const fn managed_state(&self) -> &ManagedSchemaState {
        &self.managed_state
    }

    /// Return the exact additive capability requirements bound by this artifact.
    #[must_use]
    pub const fn required_capabilities(&self) -> &CapabilitySet {
        &self.required_capabilities
    }
}

/// Build and immediately reverify one source-free schema authority.
///
/// `required_capabilities` may add workspace/runtime requirements to the
/// declaration's own requirements, but it may neither omit a declaration
/// requirement nor claim a capability absent from `context`.
pub fn build_schema_authority(
    declared: &DeclaredSchema,
    required_capabilities: &CapabilitySet,
    context: &ManagedDeltaContext,
) -> Result<VerifiedSchemaAuthority, SchemaAuthorityError> {
    declared
        .required_capabilities()
        .ensure_supported_by(required_capabilities)?;
    required_capabilities.ensure_supported_by(context.available_capabilities())?;

    let semantic_profile = SemanticProfileBinding::resolve(context.semantic_profile().clone())?;
    let managed_scope = ManagedScopeBinding::exclusive(context.scope_id().clone())
        .map_err(SchemaAuthorityError::from)?;
    let resolved = resolve_schema_with_capabilities(
        declared,
        context.semantic_profile(),
        context.available_capabilities(),
    )?;
    let managed_state = managed_schema_state(declared, context)?;
    let declared_bytes = encode_declared_schema(declared)?;
    let content = SchemaAuthorityContentWire {
        authority_version: TYPEBRIDGE_SCHEMA_AUTHORITY_V1.to_owned(),
        codec_version: CodecVersion::V1.get(),
        declared_identity: canonical_value(declared.declared_identity_fingerprint())?,
        declared_schema: from_canonical_json(&declared_bytes)?,
        managed_scope: ManagedScopeBindingWire::from_binding(&managed_scope)?,
        managed_state: canonical_value(&managed_state)?,
        required_capabilities: required_capabilities.clone(),
        schema_ir_version: declared.format().get(),
        semantic_profile: SemanticProfileBindingWire::from_binding(&semantic_profile)?,
        semantic_schema: canonical_value(resolved.semantic_fingerprint())?,
    };
    let authority_fingerprint = compute_authority_fingerprint(&content)?;
    let wire = SchemaAuthorityWire {
        authority_fingerprint: canonical_value(&authority_fingerprint)?,
        content,
    };
    let bytes = to_canonical_json(&wire)?;
    decode_schema_authority(&bytes, context.available_capabilities())
}

/// Return exact canonical bytes retained by an already verified authority.
#[must_use]
pub fn encode_schema_authority(authority: &VerifiedSchemaAuthority) -> Vec<u8> {
    authority.canonical_bytes.clone()
}

/// Decode canonical bytes and independently reconstruct every bound schema view.
///
/// The caller supplies only its available capability set. Scope and semantic
/// profile come from the artifact and are accepted only through their frozen
/// registries; no source workspace or repeated deployment configuration is
/// consulted.
pub fn decode_schema_authority(
    bytes: &[u8],
    available_capabilities: &CapabilitySet,
) -> Result<VerifiedSchemaAuthority, SchemaAuthorityError> {
    let wire: SchemaAuthorityWire = from_canonical_json(bytes)?;
    if to_canonical_json(&wire)? != bytes {
        return Err(SchemaAuthorityError::new(
            SchemaAuthorityErrorCode::Contract,
            "schema-authority bytes normalize after typed reconstruction",
        ));
    }
    if wire.content.authority_version != TYPEBRIDGE_SCHEMA_AUTHORITY_V1
        || wire.content.codec_version != CodecVersion::V1.get()
        || wire.content.schema_ir_version != FormatVersion::V1.get()
    {
        return Err(SchemaAuthorityError::new(
            SchemaAuthorityErrorCode::UnsupportedVersion,
            "schema-authority, codec, or schema-IR version is unsupported",
        ));
    }

    let authority_fingerprint = compute_authority_fingerprint(&wire.content)?;
    require_exact_value(
        &wire.authority_fingerprint,
        &authority_fingerprint,
        "schema-authority content fingerprint is stale",
    )?;

    let SchemaAuthorityContentWire {
        authority_version: _,
        codec_version: _,
        declared_identity,
        declared_schema,
        managed_scope,
        managed_state,
        required_capabilities,
        schema_ir_version: _,
        semantic_profile,
        semantic_schema,
    } = wire.content;

    required_capabilities.ensure_supported_by(available_capabilities)?;
    let semantic_profile = semantic_profile.rebuild()?;
    let managed_scope = managed_scope.rebuild()?;
    let declared_bytes = to_canonical_json(&declared_schema)?;
    let declared = decode_declared_schema(&declared_bytes)?;
    declared
        .required_capabilities()
        .ensure_supported_by(&required_capabilities)?;
    require_exact_value(
        &declared_identity,
        declared.declared_identity_fingerprint(),
        "declared-schema identity fingerprint is stale",
    )?;

    let resolved =
        resolve_schema_with_capabilities(&declared, semantic_profile.id(), available_capabilities)?;
    require_exact_value(
        &semantic_schema,
        resolved.semantic_fingerprint(),
        "global semantic-schema fingerprint is stale",
    )?;

    let managed_context = ManagedDeltaContext::new(
        managed_scope.id().clone(),
        semantic_profile.id().clone(),
        available_capabilities.clone(),
    );
    let rebuilt_managed_state = managed_schema_state(&declared, &managed_context)?;
    require_exact_value(
        &managed_state,
        &rebuilt_managed_state,
        "managed schema state is stale",
    )?;

    Ok(VerifiedSchemaAuthority {
        authority_fingerprint,
        canonical_bytes: bytes.to_vec(),
        declared,
        managed_scope,
        managed_state: rebuilt_managed_state,
        required_capabilities,
        resolved,
        semantic_profile,
    })
}

fn compute_authority_fingerprint(
    content: &SchemaAuthorityContentWire,
) -> Result<Fingerprint, SchemaAuthorityError> {
    Ok(Fingerprint::compute(
        FingerprintDomain::new(SCHEMA_AUTHORITY_FINGERPRINT_DOMAIN)?,
        CanonicalizationVersion::new(SCHEMA_AUTHORITY_FINGERPRINT_CANONICALIZATION)?,
        None,
        &to_canonical_json(content)?,
    ))
}

fn canonical_value<T: Serialize>(value: &T) -> Result<Value, SchemaAuthorityError> {
    Ok(from_canonical_json(&to_canonical_json(value)?)?)
}

fn require_exact_value<T: Serialize>(
    actual: &Value,
    expected: &T,
    message: &'static str,
) -> Result<(), SchemaAuthorityError> {
    if actual == &canonical_value(expected)? {
        Ok(())
    } else {
        Err(integrity_failure(message))
    }
}

fn integrity_failure(message: &'static str) -> SchemaAuthorityError {
    SchemaAuthorityError::new(SchemaAuthorityErrorCode::IntegrityMismatch, message)
}