Select from a consistent asserted snapshot. Limits stop traversal rather than silently treating a depth-limited neighborhood as complete lineage.
(
snapshot: &WorkflowSnapshot,
root: &str,
max_nodes: usize,
)
| 103 | /// Select from a consistent asserted snapshot. Limits stop traversal rather |
| 104 | /// than silently treating a depth-limited neighborhood as complete lineage. |
| 105 | pub fn from_snapshot( |
| 106 | snapshot: &WorkflowSnapshot, |
| 107 | root: &str, |
| 108 | max_nodes: usize, |
| 109 | ) -> Result<Self, RepositoryError> { |
| 110 | if !(1..=HARD_MAX_NODES).contains(&max_nodes) { |
| 111 | return Err(invalid("max-nodes must be between 1 and 10000")); |
| 112 | } |
| 113 | if snapshot.generation != atomic_core::kg::KG_GENERATION |
| 114 | || snapshot.ontology_version != atomic_core::kg::ontology::VERSION_IRI |
| 115 | { |
| 116 | return Err(invalid( |
| 117 | "unsupported workflow snapshot generation or ontology; rebuild the knowledge graph", |
| 118 | )); |
| 119 | } |
| 120 | iri::parse_intent(root).map_err(|error| invalid(&error.to_string()))?; |
| 121 | // These three resources are defined by the embedded ontology rather than |
| 122 | // persisted as KG rows. Include them when an asserted edge references one. |
| 123 | let statuses: Vec<_> = ["Verified", "Failed", "Unverifiable"] |
| 124 | .into_iter() |
| 125 | .map(|name| { |
| 126 | KgNode::new( |
| 127 | format!("https://atomic.dev/ns/workflow#{name}"), |
| 128 | C::VerificationStatus, |
| 129 | name, |
| 130 | "ontology", |
| 131 | ) |
| 132 | }) |
| 133 | .collect(); |
| 134 | let nodes: BTreeMap<&str, &KgNode> = snapshot |
| 135 | .nodes |
| 136 | .iter() |
| 137 | .chain(statuses.iter()) |
| 138 | .map(|n| (n.id.as_str(), n)) |
| 139 | .collect(); |
| 140 | if !nodes.get(root).is_some_and(|node| { |
| 141 | node.has_type(C::WorkIntent) |
| 142 | || node |
| 143 | .types |
| 144 | .iter() |
| 145 | .filter_map(|t| C::from_iri(t)) |
| 146 | .any(|c| c.parents().iter().any(|p| p.iri() == C::WorkIntent.iri())) |
| 147 | }) { |
| 148 | return Err(invalid("intent is not present in the workflow snapshot; run atomic query enrich to rebuild it")); |
| 149 | } |
| 150 | let mut outgoing: BTreeMap<&str, Vec<&KgEdge>> = BTreeMap::new(); |
| 151 | let mut incoming: BTreeMap<&str, Vec<&KgEdge>> = BTreeMap::new(); |
| 152 | for edge in &snapshot.edges { |
| 153 | outgoing.entry(&edge.from_id).or_default().push(edge); |
| 154 | incoming.entry(&edge.to_id).or_default().push(edge); |
| 155 | } |
| 156 | // Sorting makes capped selection independent of storage/serialization order. |
| 157 | for edges in outgoing.values_mut().chain(incoming.values_mut()) { |
| 158 | edges.sort_by(|a, b| { |
| 159 | (&a.from_id, a.kind, &a.to_id).cmp(&(&b.from_id, b.kind, &b.to_id)) |
| 160 | }); |
| 161 | } |
| 162 | let mut selected = BTreeSet::from([root.to_owned()]); |
nothing calls this directly
no test coverage detected