interplane.Get Omega
Read in your language

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

branch main Branches Tags History

aien-sovereign-core-mv / crates / cortex-path-m1 / src / score.rs

247 lines · 7726 bytes Raw Blame History
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
16use crate::config::ExperimentConfig;
17use chrono::{DateTime, Utc};
18use cortex_rs::{ClaimStatus, VerificationTier};
19use serde::Serialize;
20use std::collections::{BTreeMap, BTreeSet};
21
22#[derive(Debug, Clone)]
23pub struct EdgeView {
24 pub claim_id: String,
25 pub subject: String,
26 pub object: String,
27 pub confidence: f64,
28 pub tier: VerificationTier,
29 pub evidence_count: usize,
30 pub created_at: String,
31 pub status: ClaimStatus,
32 pub retracted: bool,
33 pub subject_space: String,
34 pub object_space: String,
35 pub claim_space: String,
36}
37
38pub fn is_admitted(edge: &EdgeView, query_space: &str) -> bool {
39 edge.status == ClaimStatus::Active
40 && !edge.retracted
41 && !edge.subject.is_empty()
42 && !edge.object.is_empty()
43 && edge.subject != edge.object
44 && edge.subject_space == query_space
45 && edge.object_space == query_space
46 && edge.claim_space == query_space
47}
48
49pub fn tier_weight(config: &ExperimentConfig, tier: VerificationTier) -> f64 {
50 match tier {
51 VerificationTier::T0Direct => config.tier_t0,
52 VerificationTier::T1Corroborated => config.tier_t1,
53 VerificationTier::T2Verified => config.tier_t2,
54 VerificationTier::T3Controlled => config.tier_t3,
55 }
56}
57
58pub fn evidence_factor(evidence_count: usize) -> f64 {
59 let n = evidence_count as f64;
60 (1.0 + n) / (2.0 + n)
61}
62
63pub fn freshness(config: &ExperimentConfig, created_at: &str, snapshot_time: DateTime<Utc>) -> f64 {
64 let Ok(created) = DateTime::parse_from_rfc3339(created_at) else {
65 return config.freshness_floor;
66 };
67 let age_days = (snapshot_time - created.with_timezone(&Utc)).num_seconds() as f64 / 86_400.0;
68 if age_days <= 0.0 {
69 return 1.0;
70 }
71 let raw = 0.5_f64.powf(age_days / config.freshness_half_life_days);
72 raw.clamp(config.freshness_floor, 1.0)
73}
74
75pub fn edge_weight(
76 config: &ExperimentConfig,
77 edge: &EdgeView,
78 snapshot_time: DateTime<Utc>,
79) -> f64 {
80 edge.confidence
81 * tier_weight(config, edge.tier)
82 * evidence_factor(edge.evidence_count)
83 * freshness(config, &edge.created_at, snapshot_time)
84}
85
86#[derive(Debug, Clone)]
87pub struct WalkEdge {
88 pub claim_id: String,
89 pub neighbor: String,
90 pub weight: f64,
91}
92
93#[derive(Debug, Clone, Serialize, PartialEq)]
94pub struct CandidateSuggestion {
95 pub query_id: String,
96 pub subject: String,
97 pub predicate: String,
98 pub object: String,
99 pub score: f64,
100}
101
102#[derive(Debug, Clone)]
103pub struct WalkOutput {
104 pub graph_score: BTreeMap<String, f64>,
105 pub paths_scored: usize,
106 pub suggestions: Vec<(String, String, f64)>,
107}
108
109pub fn walk_paths(
110 config: &ExperimentConfig,
111 anchors: &[(String, f64)],
112 adj: &BTreeMap<String, Vec<WalkEdge>>,
113 direct: &BTreeSet<(String, String)>,
114) -> WalkOutput {
115 let anchor_ids: BTreeSet<&str> = anchors.iter().map(|(id, _)| id.as_str()).collect();
116 let mut graph_score: BTreeMap<String, f64> = BTreeMap::new();
117 let mut paths_scored = 0usize;
118 let mut indirect: BTreeMap<(String, String), f64> = BTreeMap::new();
119
120 for (anchor_id, anchor_base) in anchors {
121 let Some(first_hops) = adj.get(anchor_id) else {
122 continue;
123 };
124 for edge in first_hops.iter().take(config.max_neighbors) {
125 if paths_scored >= config.max_paths {
126 break;
127 }
128 if edge.neighbor == *anchor_id {
129 continue;
130 }
131 let contribution = anchor_base * edge.weight;
132 award(config, &mut graph_score, &edge.neighbor, contribution);
133 paths_scored += 1;
134 if config.max_hops < 2 {
135 continue;
136 }
137 let Some(second_hops) = adj.get(&edge.neighbor) else {
138 continue;
139 };
140 for edge2 in second_hops.iter().take(config.max_neighbors) {
141 if paths_scored >= config.max_paths {
142 break;
143 }
144 if edge2.neighbor == *anchor_id || edge2.neighbor == edge.neighbor {
145 continue;
146 }
147 let path_score = edge.weight * edge2.weight * config.hop_decay;
148 let contribution = anchor_base * path_score;
149 award(config, &mut graph_score, &edge2.neighbor, contribution);
150 if anchor_ids.contains(edge2.neighbor.as_str()) {
151 let pair = ordered_pair(anchor_id, &edge2.neighbor);
152 let slot = indirect.entry(pair).or_insert(0.0);
153 if contribution > *slot {
154 *slot = contribution;
155 }
156 }
157 paths_scored += 1;
158 }
159 }
160 }
161
162 let suggestions = indirect
163 .into_iter()
164 .filter(|(pair, _)| !direct.contains(pair))
165 .map(|(pair, score)| (pair.0, pair.1, score))
166 .collect();
167
168 WalkOutput {
169 graph_score,
170 paths_scored,
171 suggestions,
172 }
173}
174
175fn award(
176 config: &ExperimentConfig,
177 graph_score: &mut BTreeMap<String, f64>,
178 entity: &str,
179 contribution: f64,
180) {
181 if let Some(existing) = graph_score.get_mut(entity) {
182 if contribution > *existing {
183 *existing = contribution;
184 }
185 return;
186 }
187 if graph_score.len() < config.max_graph_entities {
188 graph_score.insert(entity.to_string(), contribution);
189 }
190}
191
192pub fn ordered_pair(left: &str, right: &str) -> (String, String) {
193 if left <= right {
194 (left.to_string(), right.to_string())
195 } else {
196 (right.to_string(), left.to_string())
197 }
198}
199
200#[derive(Debug, Clone, PartialEq)]
201pub struct RankedEntity {
202 pub id: String,
203 pub baseline: f64,
204 pub graph_score: f64,
205 pub final_score: f64,
206}
207
208pub fn fuse_and_rank(
209 config: &ExperimentConfig,
210 baseline: &[(String, f64)],
211 graph_score: &BTreeMap<String, f64>,
212) -> Vec<RankedEntity> {
213 let mut ranked: Vec<RankedEntity> = baseline
214 .iter()
215 .map(|(id, baseline_score)| {
216 let graph = graph_score.get(id).copied().unwrap_or(0.0);
217 RankedEntity {
218 id: id.clone(),
219 baseline: *baseline_score,
220 graph_score: graph,
221 final_score: baseline_score + config.graph_mix * graph,
222 }
223 })
224 .collect();
225 ranked.sort_by(|left, right| {
226 right
227 .final_score
228 .partial_cmp(&left.final_score)
229 .unwrap_or(std::cmp::Ordering::Equal)
230 .then_with(|| left.id.cmp(&right.id))
231 });
232 ranked.truncate(config.top_k);
233 ranked
234}
235
236pub fn select_anchors(config: &ExperimentConfig, scored: &[(String, f64)]) -> Vec<(String, f64)> {
237 let mut ordered = scored.to_vec();
238 ordered.sort_by(|left, right| {
239 right
240 .1
241 .partial_cmp(&left.1)
242 .unwrap_or(std::cmp::Ordering::Equal)
243 .then_with(|| left.0.cmp(&right.0))
244 });
245 ordered.truncate(config.anchor_count);
246 ordered
247}