tatara-core 0.2.96

Shared domain types, config, and cluster types for tatara
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
//! Typed convergence DAGs.
//!
//! A convergence graph is a DAG of typed convergence points with typed edges.
//! Points are content-addressed via PointId. The graph supports topological
//! ordering, substrate filtering, and validation.

use serde::{Deserialize, Serialize};
use std::collections::{BTreeMap, BTreeSet, HashMap, VecDeque};

use super::compliance_binding::ComplianceClosure;
use super::convergence_state::{ConvergencePoint, SubstrateType};
use super::multi_distance::ConvergenceBandwidth;
use super::point_id::PointId;

/// The type of relationship between two convergence points.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum EdgeType {
    /// Data flows from one point to another.
    Data,
    /// Control dependency (must complete before next can start).
    Control,
    /// Attestation chain (output attestation feeds input attestation).
    Attestation,
}

/// A typed edge between two convergence points.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TypedEdge {
    /// Source point.
    pub from: PointId,
    /// Target point.
    pub to: PointId,
    /// Type of relationship.
    pub edge_type: EdgeType,
}

/// The complete convergence graph across all substrates.
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct ConvergenceGraph {
    /// All points, keyed by hex-encoded PointId.
    /// (BTreeMap keys must be strings for JSON serialization.)
    #[serde(with = "point_id_map")]
    pub points: BTreeMap<PointId, ConvergencePoint>,
    /// Typed edges between points.
    pub edges: Vec<TypedEdge>,
}

mod point_id_map {
    use super::*;
    use serde::de::{self, MapAccess, Visitor};
    use serde::ser::SerializeMap;

    pub fn serialize<S>(
        map: &BTreeMap<PointId, ConvergencePoint>,
        serializer: S,
    ) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        let mut m = serializer.serialize_map(Some(map.len()))?;
        for (k, v) in map {
            m.serialize_entry(&k.to_hex(), v)?;
        }
        m.end()
    }

    pub fn deserialize<'de, D>(
        deserializer: D,
    ) -> Result<BTreeMap<PointId, ConvergencePoint>, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        struct PointIdMapVisitor;
        impl<'de> Visitor<'de> for PointIdMapVisitor {
            type Value = BTreeMap<PointId, ConvergencePoint>;
            fn expecting(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
                f.write_str("a map with hex PointId keys")
            }
            fn visit_map<M: MapAccess<'de>>(self, mut access: M) -> Result<Self::Value, M::Error> {
                let mut map = BTreeMap::new();
                while let Some((key, value)) = access.next_entry::<String, ConvergencePoint>()? {
                    let id = PointId::from_hex(&key).map_err(de::Error::custom)?;
                    map.insert(id, value);
                }
                Ok(map)
            }
        }
        deserializer.deserialize_map(PointIdMapVisitor)
    }
}

impl ConvergenceGraph {
    /// Create an empty graph.
    pub fn new() -> Self {
        Self::default()
    }

    /// Add a convergence point, returning its PointId.
    pub fn add_point(&mut self, id: PointId, point: ConvergencePoint) {
        self.points.insert(id, point);
    }

    /// Add a typed edge between two points.
    pub fn add_edge(&mut self, edge: TypedEdge) {
        self.edges.push(edge);
    }

    /// Compute topological order using Kahn's algorithm.
    /// Returns Err if the graph contains a cycle.
    pub fn topological_order(&self) -> Result<Vec<PointId>, GraphError> {
        let mut in_degree: HashMap<PointId, usize> = HashMap::new();
        let mut adjacency: HashMap<PointId, Vec<PointId>> = HashMap::new();

        for id in self.points.keys() {
            in_degree.entry(*id).or_insert(0);
            adjacency.entry(*id).or_default();
        }

        for edge in &self.edges {
            *in_degree.entry(edge.to).or_insert(0) += 1;
            adjacency.entry(edge.from).or_default().push(edge.to);
        }

        let mut queue: VecDeque<PointId> = in_degree
            .iter()
            .filter(|(_, &deg)| deg == 0)
            .map(|(id, _)| *id)
            .collect();

        let mut order = Vec::new();

        while let Some(id) = queue.pop_front() {
            order.push(id);
            if let Some(neighbors) = adjacency.get(&id) {
                for neighbor in neighbors {
                    if let Some(deg) = in_degree.get_mut(neighbor) {
                        *deg -= 1;
                        if *deg == 0 {
                            queue.push_back(*neighbor);
                        }
                    }
                }
            }
        }

        if order.len() != self.points.len() {
            Err(GraphError::CycleDetected)
        } else {
            Ok(order)
        }
    }

    /// Validate the graph: all edge endpoints exist, no cycles.
    pub fn validate(&self) -> Result<(), GraphError> {
        for edge in &self.edges {
            if !self.points.contains_key(&edge.from) {
                return Err(GraphError::MissingPoint(edge.from));
            }
            if !self.points.contains_key(&edge.to) {
                return Err(GraphError::MissingPoint(edge.to));
            }
        }
        self.topological_order()?;
        Ok(())
    }

    /// Filter points by substrate type.
    pub fn points_by_substrate(
        &self,
        substrate: &SubstrateType,
    ) -> Vec<(&PointId, &ConvergencePoint)> {
        self.points
            .iter()
            .filter(|(_, p)| &p.substrate == substrate)
            .collect()
    }

    /// Get the number of points.
    pub fn point_count(&self) -> usize {
        self.points.len()
    }

    /// Get the number of edges.
    pub fn edge_count(&self) -> usize {
        self.edges.len()
    }

    /// Compute forward closure: all points this point transitively depends on.
    /// Walks backward along edges (from → to) to find all upstream dependencies.
    /// Like `nix-store --query --requisites`.
    pub fn forward_closure(&self, point_id: &PointId) -> BTreeSet<PointId> {
        let mut adjacency: HashMap<PointId, Vec<PointId>> = HashMap::new();
        for edge in &self.edges {
            adjacency.entry(edge.to).or_default().push(edge.from);
        }

        let mut visited = BTreeSet::new();
        let mut queue = VecDeque::new();
        queue.push_back(*point_id);

        while let Some(id) = queue.pop_front() {
            if !visited.insert(id) {
                continue;
            }
            if let Some(deps) = adjacency.get(&id) {
                for dep in deps {
                    queue.push_back(*dep);
                }
            }
        }

        visited.remove(point_id);
        visited
    }

    /// Compute reverse closure: all points that transitively depend on this point.
    /// Walks forward along edges (from → to) to find all downstream dependents.
    /// Like `nix-store --query --referrers` (transitive).
    pub fn reverse_closure(&self, point_id: &PointId) -> BTreeSet<PointId> {
        let mut adjacency: HashMap<PointId, Vec<PointId>> = HashMap::new();
        for edge in &self.edges {
            adjacency.entry(edge.from).or_default().push(edge.to);
        }

        let mut visited = BTreeSet::new();
        let mut queue = VecDeque::new();
        queue.push_back(*point_id);

        while let Some(id) = queue.pop_front() {
            if !visited.insert(id) {
                continue;
            }
            if let Some(deps) = adjacency.get(&id) {
                for dep in deps {
                    queue.push_back(*dep);
                }
            }
        }

        visited.remove(point_id);
        visited
    }
}

/// A substrate-scoped subgraph with cross-substrate boundary edges.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SubstrateDAG {
    /// The substrate this DAG belongs to.
    pub substrate: SubstrateType,
    /// Points in this substrate.
    pub points: BTreeMap<PointId, ConvergencePoint>,
    /// Edges within this substrate.
    pub internal_edges: Vec<TypedEdge>,
    /// Edges crossing to other substrates.
    pub cross_edges: Vec<TypedEdge>,
    /// Maximum convergence velocity for this substrate.
    pub bandwidth: ConvergenceBandwidth,
}

/// Pre-execution analysis of a convergence graph.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConvergencePlan {
    /// The graph being planned.
    pub graph: ConvergenceGraph,
    /// Topological execution order.
    pub execution_order: Vec<PointId>,
    /// Compliance bindings resolved to specific points.
    pub compliance: ComplianceClosure,
    /// Points that can skip re-execution (attestation unchanged).
    pub cache_hits: Vec<PointId>,
    /// Longest sequential dependency chain.
    pub critical_path: Vec<PointId>,
}

/// Errors in convergence graph operations.
#[derive(Debug, Clone, thiserror::Error)]
pub enum GraphError {
    #[error("cycle detected in convergence graph")]
    CycleDetected,
    #[error("edge references missing point: {0}")]
    MissingPoint(PointId),
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::domain::convergence_state::*;

    fn make_point(name: &str, substrate: SubstrateType) -> (PointId, ConvergencePoint) {
        let id = PointId::compute(name.as_bytes(), &[], b"desired");
        let point = ConvergencePoint {
            name: name.into(),
            description: format!("{name} point"),
            monotone: true,
            mechanism: ConvergenceMechanism::Local,
            state: ConvergenceState::new(name),
            boundary: ConvergenceBoundary::default(),
            point_type: ConvergencePointType::Transform,
            horizon: ConvergenceHorizon::Bounded,
            substrate,
            computation_mode: ComputationMode::Mechanical,
        };
        (id, point)
    }

    #[test]
    fn test_empty_graph() {
        let g = ConvergenceGraph::new();
        assert_eq!(g.point_count(), 0);
        assert!(g.validate().is_ok());
        assert!(g.topological_order().unwrap().is_empty());
    }

    #[test]
    fn test_single_point() {
        let mut g = ConvergenceGraph::new();
        let (id, p) = make_point("a", SubstrateType::Compute);
        g.add_point(id, p);
        assert_eq!(g.point_count(), 1);
        assert!(g.validate().is_ok());
        assert_eq!(g.topological_order().unwrap(), vec![id]);
    }

    #[test]
    fn test_linear_chain() {
        let mut g = ConvergenceGraph::new();
        let (a, pa) = make_point("a", SubstrateType::Compute);
        let (b, pb) = make_point("b", SubstrateType::Compute);
        let (c, pc) = make_point("c", SubstrateType::Compute);
        g.add_point(a, pa);
        g.add_point(b, pb);
        g.add_point(c, pc);
        g.add_edge(TypedEdge {
            from: a,
            to: b,
            edge_type: EdgeType::Attestation,
        });
        g.add_edge(TypedEdge {
            from: b,
            to: c,
            edge_type: EdgeType::Attestation,
        });

        let order = g.topological_order().unwrap();
        assert_eq!(order.len(), 3);
        let pos_a = order.iter().position(|x| *x == a).unwrap();
        let pos_b = order.iter().position(|x| *x == b).unwrap();
        let pos_c = order.iter().position(|x| *x == c).unwrap();
        assert!(pos_a < pos_b);
        assert!(pos_b < pos_c);
    }

    #[test]
    fn test_cycle_detection() {
        let mut g = ConvergenceGraph::new();
        let (a, pa) = make_point("a", SubstrateType::Compute);
        let (b, pb) = make_point("b", SubstrateType::Compute);
        g.add_point(a, pa);
        g.add_point(b, pb);
        g.add_edge(TypedEdge {
            from: a,
            to: b,
            edge_type: EdgeType::Data,
        });
        g.add_edge(TypedEdge {
            from: b,
            to: a,
            edge_type: EdgeType::Data,
        });

        assert!(matches!(
            g.topological_order(),
            Err(GraphError::CycleDetected)
        ));
    }

    #[test]
    fn test_missing_point_validation() {
        let mut g = ConvergenceGraph::new();
        let (a, pa) = make_point("a", SubstrateType::Compute);
        let missing = PointId::compute(b"missing", &[], b"state");
        g.add_point(a, pa);
        g.add_edge(TypedEdge {
            from: a,
            to: missing,
            edge_type: EdgeType::Data,
        });

        assert!(matches!(g.validate(), Err(GraphError::MissingPoint(_))));
    }

    #[test]
    fn test_points_by_substrate() {
        let mut g = ConvergenceGraph::new();
        let (a, pa) = make_point("a", SubstrateType::Compute);
        let (b, pb) = make_point("b", SubstrateType::Security);
        let (c, pc) = make_point("c", SubstrateType::Compute);
        g.add_point(a, pa);
        g.add_point(b, pb);
        g.add_point(c, pc);

        assert_eq!(g.points_by_substrate(&SubstrateType::Compute).len(), 2);
        assert_eq!(g.points_by_substrate(&SubstrateType::Security).len(), 1);
        assert_eq!(g.points_by_substrate(&SubstrateType::Financial).len(), 0);
    }

    #[test]
    fn test_forward_closure() {
        let mut g = ConvergenceGraph::new();
        let (a, pa) = make_point("a", SubstrateType::Compute);
        let (b, pb) = make_point("b", SubstrateType::Compute);
        let (c, pc) = make_point("c", SubstrateType::Compute);
        g.add_point(a, pa);
        g.add_point(b, pb);
        g.add_point(c, pc);
        g.add_edge(TypedEdge {
            from: a,
            to: b,
            edge_type: EdgeType::Data,
        });
        g.add_edge(TypedEdge {
            from: b,
            to: c,
            edge_type: EdgeType::Data,
        });

        let closure = g.forward_closure(&c);
        assert!(closure.contains(&a));
        assert!(closure.contains(&b));
        assert!(!closure.contains(&c));
    }

    #[test]
    fn test_reverse_closure() {
        let mut g = ConvergenceGraph::new();
        let (a, pa) = make_point("a", SubstrateType::Compute);
        let (b, pb) = make_point("b", SubstrateType::Compute);
        let (c, pc) = make_point("c", SubstrateType::Compute);
        g.add_point(a, pa);
        g.add_point(b, pb);
        g.add_point(c, pc);
        g.add_edge(TypedEdge {
            from: a,
            to: b,
            edge_type: EdgeType::Data,
        });
        g.add_edge(TypedEdge {
            from: b,
            to: c,
            edge_type: EdgeType::Data,
        });

        let closure = g.reverse_closure(&a);
        assert!(closure.contains(&b));
        assert!(closure.contains(&c));
        assert!(!closure.contains(&a));
    }

    #[test]
    fn test_diamond_dag() {
        let mut g = ConvergenceGraph::new();
        let (a, pa) = make_point("a", SubstrateType::Compute);
        let (b, pb) = make_point("b", SubstrateType::Compute);
        let (c, pc) = make_point("c", SubstrateType::Compute);
        let (d, pd) = make_point("d", SubstrateType::Compute);
        g.add_point(a, pa);
        g.add_point(b, pb);
        g.add_point(c, pc);
        g.add_point(d, pd);
        g.add_edge(TypedEdge {
            from: a,
            to: b,
            edge_type: EdgeType::Data,
        });
        g.add_edge(TypedEdge {
            from: a,
            to: c,
            edge_type: EdgeType::Data,
        });
        g.add_edge(TypedEdge {
            from: b,
            to: d,
            edge_type: EdgeType::Data,
        });
        g.add_edge(TypedEdge {
            from: c,
            to: d,
            edge_type: EdgeType::Data,
        });

        let order = g.topological_order().unwrap();
        assert_eq!(order.len(), 4);
        let pos_a = order.iter().position(|x| *x == a).unwrap();
        let pos_d = order.iter().position(|x| *x == d).unwrap();
        assert!(pos_a < pos_d);
    }

    #[test]
    fn test_graph_serde() {
        let mut g = ConvergenceGraph::new();
        let (a, pa) = make_point("a", SubstrateType::Compute);
        let (b, pb) = make_point("b", SubstrateType::Network);
        g.add_point(a, pa);
        g.add_point(b, pb);
        g.add_edge(TypedEdge {
            from: a,
            to: b,
            edge_type: EdgeType::Control,
        });

        let json = serde_json::to_string(&g).unwrap();
        let parsed: ConvergenceGraph = serde_json::from_str(&json).unwrap();
        assert_eq!(parsed.point_count(), 2);
        assert_eq!(parsed.edge_count(), 1);
    }
}