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
//! Provider-neutral fact dependency planning for schema deltas.

use std::collections::{BTreeMap, BTreeSet};

use type_bridge_contract::diagnostic::{Diagnostic, DiagnosticCategory, DiagnosticCode};
use type_bridge_contract::id::{Label, RoleId, TypeId, TypeKind};
use type_bridge_contract::schema::{
    AnnotationSubjectId, DeclaredSchema, RelatesFactId, SchemaFact, SchemaFactId, SchemaOperation,
    SubFactId, ValueFactId,
};

/// The exact fact-level dependency graph used to order schema operations.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FactDependencyGraph {
    dependencies: BTreeMap<SchemaFactId, BTreeSet<SchemaFactId>>,
    dependents: BTreeMap<SchemaFactId, BTreeSet<SchemaFactId>>,
}

impl FactDependencyGraph {
    /// Build the graph from a complete declared fact inventory.
    pub fn from_declared(declared: &DeclaredSchema) -> Result<Self, Diagnostic> {
        Self::from_facts(declared.facts())
    }

    /// Build the graph from a complete fact inventory.
    pub fn from_facts<'a>(
        facts: impl IntoIterator<Item = &'a SchemaFact>,
    ) -> Result<Self, Diagnostic> {
        let mut inventory = BTreeMap::new();
        for fact in facts {
            let id = fact.id();
            if inventory.insert(id.clone(), fact.clone()).is_some() {
                return Err(failure(
                    "schema_delta_duplicate_fact",
                    format!("duplicate fact identity in dependency inventory: {id:?}"),
                ));
            }
        }

        let mut labels: BTreeMap<Label, Vec<SchemaFactId>> = BTreeMap::new();
        let mut sub_by_child: BTreeMap<TypeId, Vec<SubFactId>> = BTreeMap::new();
        for fact in inventory.values() {
            match fact {
                SchemaFact::Type(fact) => labels
                    .entry(fact.id().label().clone())
                    .or_default()
                    .push(SchemaFactId::Type(fact.id().clone())),
                SchemaFact::Struct(fact) => labels
                    .entry(fact.id().label().clone())
                    .or_default()
                    .push(SchemaFactId::Struct(fact.id().clone())),
                SchemaFact::Sub(fact) => sub_by_child
                    .entry(fact.id().subtype().clone())
                    .or_default()
                    .push(fact.id().clone()),
                _ => {}
            }
        }
        for candidates in labels.values_mut() {
            candidates.sort();
        }
        for edges in sub_by_child.values_mut() {
            edges.sort();
        }

        let mut dependencies = BTreeMap::new();
        for fact in inventory.values() {
            dependencies.insert(fact.id(), dependencies_for(fact, &labels, &sub_by_child)?);
        }

        let mut dependents: BTreeMap<SchemaFactId, BTreeSet<SchemaFactId>> = BTreeMap::new();
        for id in dependencies.keys() {
            dependents.entry(id.clone()).or_default();
        }
        for (dependent, prerequisites) in &dependencies {
            for prerequisite in prerequisites {
                dependents
                    .entry(prerequisite.clone())
                    .or_default()
                    .insert(dependent.clone());
            }
        }

        Ok(Self {
            dependencies,
            dependents,
        })
    }

    /// Return the direct prerequisites of one fact.
    #[must_use]
    pub fn dependencies(&self, id: &SchemaFactId) -> Option<&BTreeSet<SchemaFactId>> {
        self.dependencies.get(id)
    }

    /// Return the direct dependents of one fact.
    #[must_use]
    pub fn dependents(&self, id: &SchemaFactId) -> Option<&BTreeSet<SchemaFactId>> {
        self.dependents.get(id)
    }

    /// Reject an inventory with a dependency outside the inventory.
    pub fn validate_complete(&self) -> Result<(), Diagnostic> {
        for (dependent, prerequisites) in &self.dependencies {
            for prerequisite in prerequisites {
                if !self.dependencies.contains_key(prerequisite) {
                    return Err(failure(
                        "schema_delta_missing_dependency",
                        format!("fact {dependent:?} requires absent fact {prerequisite:?}"),
                    ));
                }
            }
        }
        Ok(())
    }
}

/// Compute the exact formal diff and deterministic dependency-safe operation order.
pub fn plan_schema_operations(
    source: &DeclaredSchema,
    target: &DeclaredSchema,
) -> Result<Vec<SchemaOperation>, Diagnostic> {
    let source_facts = fact_map(source);
    let target_facts = fact_map(target);
    let source_graph = FactDependencyGraph::from_declared(source)?;
    let target_graph = FactDependencyGraph::from_declared(target)?;
    source_graph.validate_complete()?;
    target_graph.validate_complete()?;

    let source_ids: BTreeSet<_> = source_facts.keys().cloned().collect();
    let target_ids: BTreeSet<_> = target_facts.keys().cloned().collect();
    let added: BTreeSet<_> = target_ids.difference(&source_ids).cloned().collect();
    let removed: BTreeSet<_> = source_ids.difference(&target_ids).cloned().collect();
    let redefined: BTreeSet<_> = source_ids
        .intersection(&target_ids)
        .filter(|id| source_facts.get(*id) != target_facts.get(*id))
        .cloned()
        .collect();

    for id in added.iter().chain(removed.iter()).chain(redefined.iter()) {
        if matches!(
            source_facts.get(id).or_else(|| target_facts.get(id)),
            Some(SchemaFact::Function(_))
        ) {
            return Err(failure(
                "unsupported_function_migration",
                "automatic migration of opaque function bodies is unsupported",
            ));
        }
    }

    let mut operations = Vec::new();

    // Establish all new identities before replacements can refer to them.
    for component in ordered_components(&target_graph, &added) {
        let facts = component
            .into_iter()
            .map(|id| target_facts[&id].clone())
            .collect();
        operations.push(SchemaOperation::define(facts)?);
    }

    // Replacements retain their identity, so SCC members can be emitted in stable ID order.
    for component in ordered_components(&target_graph, &redefined) {
        for id in component {
            operations.push(SchemaOperation::redefine(
                source_facts[&id].clone(),
                target_facts[&id].clone(),
            )?);
        }
    }

    // Dependents must disappear before their prerequisites.
    for component in ordered_components(&source_graph, &removed)
        .into_iter()
        .rev()
    {
        for id in component.into_iter().rev() {
            operations.push(SchemaOperation::undefine(source_facts[&id].clone()));
        }
    }

    Ok(operations)
}

fn fact_map(declared: &DeclaredSchema) -> BTreeMap<SchemaFactId, SchemaFact> {
    declared
        .facts()
        .map(|fact| (fact.id(), fact.clone()))
        .collect()
}

fn dependencies_for(
    fact: &SchemaFact,
    labels: &BTreeMap<Label, Vec<SchemaFactId>>,
    sub_by_child: &BTreeMap<TypeId, Vec<SubFactId>>,
) -> Result<BTreeSet<SchemaFactId>, Diagnostic> {
    let mut dependencies = BTreeSet::new();
    match fact {
        SchemaFact::Type(_) | SchemaFact::Struct(_) => {}
        SchemaFact::Sub(fact) => {
            dependencies.insert(SchemaFactId::Type(fact.id().subtype().clone()));
            dependencies.insert(SchemaFactId::Type(fact.id().supertype().clone()));
        }
        SchemaFact::Value(fact) => {
            dependencies.insert(SchemaFactId::Type(attribute_type(fact.id().attribute())?));
        }
        SchemaFact::Owns(fact) => {
            dependencies.insert(SchemaFactId::Type(fact.id().owner().clone()));
            dependencies.insert(SchemaFactId::Type(attribute_type(fact.id().attribute())?));
            dependencies.insert(SchemaFactId::Value(ValueFactId::new(
                fact.id().attribute().clone(),
            )));
        }
        SchemaFact::Relates(fact) => {
            dependencies.insert(SchemaFactId::Type(fact.id().relation().clone()));
            if let Some(parent_role) = fact.specializes() {
                dependencies.insert(SchemaFactId::Relates(relates_id(parent_role)?));
                let parent_relation = declaring_relation_type(parent_role)?;
                let path = find_sub_path(
                    fact.id().relation(),
                    &parent_relation,
                    sub_by_child,
                    &mut BTreeSet::new(),
                )
                .ok_or_else(|| {
                    failure(
                        "schema_delta_missing_specialization_path",
                        format!(
                            "role specialization has no subtype path from {:?} to {parent_relation:?}",
                            fact.id().relation()
                        ),
                    )
                })?;
                dependencies.extend(path.into_iter().map(SchemaFactId::Sub));
            }
        }
        SchemaFact::Plays(fact) => {
            dependencies.insert(SchemaFactId::Type(fact.id().player().clone()));
            dependencies.insert(SchemaFactId::Relates(relates_id(fact.id().role())?));
        }
        SchemaFact::Annotation(fact) => {
            dependencies.insert(annotation_subject_id(fact.id().subject()));
        }
        SchemaFact::Function(fact) => {
            for label in fact.schema_references() {
                let candidates = labels.get(label).ok_or_else(|| {
                    failure(
                        "schema_delta_missing_function_reference",
                        format!("function references absent schema label {label:?}"),
                    )
                })?;
                if candidates.len() != 1 {
                    return Err(failure(
                        "schema_delta_ambiguous_function_reference",
                        format!("function schema label {label:?} is ambiguous"),
                    ));
                }
                dependencies.insert(candidates[0].clone());
            }
        }
    }
    Ok(dependencies)
}

fn annotation_subject_id(subject: &AnnotationSubjectId) -> SchemaFactId {
    match subject {
        AnnotationSubjectId::Type(id) => SchemaFactId::Type(id.clone()),
        AnnotationSubjectId::Sub(id) => SchemaFactId::Sub(id.clone()),
        AnnotationSubjectId::Value(id) => SchemaFactId::Value(id.clone()),
        AnnotationSubjectId::Owns(id) => SchemaFactId::Owns(id.clone()),
        AnnotationSubjectId::Relates(id) => SchemaFactId::Relates(id.clone()),
        AnnotationSubjectId::Plays(id) => SchemaFactId::Plays(id.clone()),
        AnnotationSubjectId::Function(id) => SchemaFactId::Function(id.clone()),
    }
}

fn attribute_type(attribute: &type_bridge_contract::id::AttributeId) -> Result<TypeId, Diagnostic> {
    TypeId::new(TypeKind::Attribute, attribute.label().as_str())
}

fn declaring_relation_type(role: &RoleId) -> Result<TypeId, Diagnostic> {
    TypeId::new(TypeKind::Relation, role.declaring_relation().as_str())
}

fn relates_id(role: &RoleId) -> Result<RelatesFactId, Diagnostic> {
    RelatesFactId::new(declaring_relation_type(role)?, role.clone())
}

fn find_sub_path(
    current: &TypeId,
    target: &TypeId,
    sub_by_child: &BTreeMap<TypeId, Vec<SubFactId>>,
    visited: &mut BTreeSet<TypeId>,
) -> Option<Vec<SubFactId>> {
    if current == target {
        return Some(Vec::new());
    }
    if !visited.insert(current.clone()) {
        return None;
    }
    for edge in sub_by_child.get(current).into_iter().flatten() {
        if let Some(mut tail) = find_sub_path(edge.supertype(), target, sub_by_child, visited) {
            let mut path = vec![edge.clone()];
            path.append(&mut tail);
            return Some(path);
        }
    }
    None
}

fn ordered_components(
    graph: &FactDependencyGraph,
    nodes: &BTreeSet<SchemaFactId>,
) -> Vec<Vec<SchemaFactId>> {
    if nodes.is_empty() {
        return Vec::new();
    }
    let components = strongly_connected_components(graph, nodes);
    let mut component_of = BTreeMap::new();
    for (index, component) in components.iter().enumerate() {
        for node in component {
            component_of.insert(node.clone(), index);
        }
    }

    let mut prerequisites = vec![BTreeSet::new(); components.len()];
    let mut dependents = vec![BTreeSet::new(); components.len()];
    for (index, component) in components.iter().enumerate() {
        for node in component {
            for dependency in graph.dependencies(node).into_iter().flatten() {
                if let Some(dependency_index) = component_of.get(dependency).copied()
                    && dependency_index != index
                {
                    prerequisites[index].insert(dependency_index);
                    dependents[dependency_index].insert(index);
                }
            }
        }
    }

    let keys: Vec<_> = components
        .iter()
        .map(|component| component[0].clone())
        .collect();
    let mut remaining: Vec<_> = prerequisites.iter().map(BTreeSet::len).collect();
    let mut ready = BTreeSet::new();
    for (index, count) in remaining.iter().enumerate() {
        if *count == 0 {
            ready.insert((keys[index].clone(), index));
        }
    }

    let mut ordered = Vec::with_capacity(components.len());
    while let Some(entry) = ready.iter().next().cloned() {
        ready.remove(&entry);
        let index = entry.1;
        ordered.push(components[index].clone());
        for dependent in dependents[index].iter().copied() {
            remaining[dependent] -= 1;
            if remaining[dependent] == 0 {
                ready.insert((keys[dependent].clone(), dependent));
            }
        }
    }
    ordered
}

fn strongly_connected_components(
    graph: &FactDependencyGraph,
    nodes: &BTreeSet<SchemaFactId>,
) -> Vec<Vec<SchemaFactId>> {
    struct Tarjan {
        next_index: usize,
        stack: Vec<SchemaFactId>,
        on_stack: BTreeSet<SchemaFactId>,
        indices: BTreeMap<SchemaFactId, usize>,
        lowlinks: BTreeMap<SchemaFactId, usize>,
        components: Vec<Vec<SchemaFactId>>,
    }

    fn visit(
        node: SchemaFactId,
        graph: &FactDependencyGraph,
        nodes: &BTreeSet<SchemaFactId>,
        state: &mut Tarjan,
    ) {
        let index = state.next_index;
        state.next_index += 1;
        state.indices.insert(node.clone(), index);
        state.lowlinks.insert(node.clone(), index);
        state.stack.push(node.clone());
        state.on_stack.insert(node.clone());

        let neighbors: Vec<_> = graph
            .dependencies(&node)
            .into_iter()
            .flatten()
            .filter(|neighbor| nodes.contains(*neighbor))
            .cloned()
            .collect();
        for neighbor in neighbors {
            if !state.indices.contains_key(&neighbor) {
                visit(neighbor.clone(), graph, nodes, state);
                let neighbor_lowlink = state.lowlinks[&neighbor];
                let lowlink = state.lowlinks.get_mut(&node).expect("visited node");
                *lowlink = (*lowlink).min(neighbor_lowlink);
            } else if state.on_stack.contains(&neighbor) {
                let neighbor_index = state.indices[&neighbor];
                let lowlink = state.lowlinks.get_mut(&node).expect("visited node");
                *lowlink = (*lowlink).min(neighbor_index);
            }
        }

        if state.lowlinks[&node] == state.indices[&node] {
            let mut component = Vec::new();
            loop {
                let member = state.stack.pop().expect("SCC root is on stack");
                state.on_stack.remove(&member);
                component.push(member.clone());
                if member == node {
                    break;
                }
            }
            component.sort();
            state.components.push(component);
        }
    }

    let mut state = Tarjan {
        next_index: 0,
        stack: Vec::new(),
        on_stack: BTreeSet::new(),
        indices: BTreeMap::new(),
        lowlinks: BTreeMap::new(),
        components: Vec::new(),
    };
    for node in nodes {
        if !state.indices.contains_key(node) {
            visit(node.clone(), graph, nodes, &mut state);
        }
    }
    state.components
}

fn failure(code: &'static str, message: impl Into<String>) -> Diagnostic {
    Diagnostic::new(
        DiagnosticCategory::Integrity,
        DiagnosticCode::new(code).expect("static diagnostic code is canonical"),
        message,
    )
}