aien-sovereign-core-mv
AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination.
Brought by drakestapleton
aien-sovereign-core-mv / crates / cortex-path-m1 / src / load.rs
| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | |
| 16 | use crate::config::ExperimentConfig; |
| 17 | use crate::score::{edge_weight, is_admitted, ordered_pair, EdgeView, WalkEdge}; |
| 18 | use chrono::{DateTime, Utc}; |
| 19 | use cortex_rs::Database; |
| 20 | use std::collections::{BTreeMap, BTreeSet, HashMap}; |
| 21 | |
| 22 | pub struct LoadedGraph { |
| 23 | pub edges: usize, |
| 24 | pub adjacency: BTreeMap<String, Vec<WalkEdge>>, |
| 25 | pub direct: BTreeSet<(String, String)>, |
| 26 | } |
| 27 | |
| 28 | pub fn load_admitted_graph( |
| 29 | db: &Database, |
| 30 | space: &str, |
| 31 | entity_ids: &[String], |
| 32 | config: &ExperimentConfig, |
| 33 | snapshot_time: DateTime<Utc>, |
| 34 | ) -> Result<LoadedGraph, String> { |
| 35 | let mut seen_claims = BTreeSet::new(); |
| 36 | let mut admitted_claims = 0usize; |
| 37 | let mut raw: Vec<(String, String, WalkEdge)> = Vec::new(); |
| 38 | let mut direct = BTreeSet::new(); |
| 39 | let mut space_of: HashMap<String, String> = HashMap::new(); |
| 40 | |
| 41 | for entity_id in entity_ids { |
| 42 | let claims = db |
| 43 | .traverse_claims(entity_id, Some(space)) |
| 44 | .map_err(|err| err.to_string())?; |
| 45 | for claim in claims { |
| 46 | if !seen_claims.insert(claim.id.clone()) { |
| 47 | continue; |
| 48 | } |
| 49 | let Some(object_id) = claim.object_entity_id.clone() else { |
| 50 | continue; |
| 51 | }; |
| 52 | let subject_space = entity_space(db, &claim.subject_entity_id, space, &mut space_of)?; |
| 53 | let object_space = entity_space(db, &object_id, space, &mut space_of)?; |
| 54 | let view = EdgeView { |
| 55 | claim_id: claim.id.clone(), |
| 56 | subject: claim.subject_entity_id.clone(), |
| 57 | object: object_id.clone(), |
| 58 | confidence: claim.confidence, |
| 59 | tier: claim.verification_tier, |
| 60 | evidence_count: claim.evidence_count, |
| 61 | created_at: claim.created_at.clone(), |
| 62 | status: claim.status, |
| 63 | retracted: claim.retracted, |
| 64 | subject_space, |
| 65 | object_space, |
| 66 | claim_space: claim.space_slug.clone(), |
| 67 | }; |
| 68 | if !is_admitted(&view, space) { |
| 69 | continue; |
| 70 | } |
| 71 | let weight = edge_weight(config, &view, snapshot_time); |
| 72 | admitted_claims += 1; |
| 73 | direct.insert(ordered_pair(&view.subject, &view.object)); |
| 74 | raw.push(( |
| 75 | view.subject.clone(), |
| 76 | view.object.clone(), |
| 77 | WalkEdge { |
| 78 | claim_id: view.claim_id.clone(), |
| 79 | neighbor: view.object.clone(), |
| 80 | weight, |
| 81 | }, |
| 82 | )); |
| 83 | raw.push(( |
| 84 | view.object.clone(), |
| 85 | view.subject.clone(), |
| 86 | WalkEdge { |
| 87 | claim_id: view.claim_id, |
| 88 | neighbor: view.subject, |
| 89 | weight, |
| 90 | }, |
| 91 | )); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | let mut adjacency: BTreeMap<String, Vec<WalkEdge>> = BTreeMap::new(); |
| 96 | for (from, _to, edge) in raw { |
| 97 | adjacency.entry(from).or_default().push(edge); |
| 98 | } |
| 99 | for neighbors in adjacency.values_mut() { |
| 100 | neighbors.sort_by(|left, right| { |
| 101 | right |
| 102 | .weight |
| 103 | .partial_cmp(&left.weight) |
| 104 | .unwrap_or(std::cmp::Ordering::Equal) |
| 105 | .then_with(|| left.claim_id.cmp(&right.claim_id)) |
| 106 | .then_with(|| left.neighbor.cmp(&right.neighbor)) |
| 107 | }); |
| 108 | } |
| 109 | |
| 110 | let edges = admitted_claims; |
| 111 | Ok(LoadedGraph { |
| 112 | edges, |
| 113 | adjacency, |
| 114 | direct, |
| 115 | }) |
| 116 | } |
| 117 | |
| 118 | fn entity_space( |
| 119 | db: &Database, |
| 120 | entity_id: &str, |
| 121 | query_space: &str, |
| 122 | cache: &mut HashMap<String, String>, |
| 123 | ) -> Result<String, String> { |
| 124 | if let Some(found) = cache.get(entity_id) { |
| 125 | return Ok(found.clone()); |
| 126 | } |
| 127 | let found = db |
| 128 | .get_entity(entity_id, Some(query_space)) |
| 129 | .map_err(|err| err.to_string())? |
| 130 | .map(|entity| entity.space_slug) |
| 131 | .unwrap_or_default(); |
| 132 | cache.insert(entity_id.to_string(), found.clone()); |
| 133 | Ok(found) |
| 134 | } |