MCPcopy Create free account
hub / github.com/atomicdotdev/atomic / from_snapshot

Method from_snapshot

atomic-repository/src/intent_graph.rs:96–404  ·  view source on GitHub ↗

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,
    )

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 15

parse_intentFunction · 0.85
intent_artifact_idFunction · 0.85
has_typeMethod · 0.80
iriMethod · 0.80
contains_keyMethod · 0.80
as_refMethod · 0.80
invalidFunction · 0.70
getMethod · 0.65
containsMethod · 0.45
collectMethod · 0.45
into_iterMethod · 0.45
iterMethod · 0.45

Tested by

no test coverage detected