Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienos.com/blob/main/crates/site-builder/src/facts.rs # aienos.com Source of www.aienos.com, served from my own hardware Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienos.com / crates / site-builder / src / facts.rs` 377 lines ยท 15825 bytes Raw Blame History 1 //! Live facts for the site. `facts` gathers them from GitHub through the `gh` CLI and writes 2 //! `facts.json`; `build` reads that file and renders it. Nothing on the page that can change is 3 //! typed by hand: it either comes from here or it is not on the page. 4 5 use serde::{Deserialize, Serialize}; 6 use std::path::Path; 7 use std::process::Command; 8 9 /// Editorial part: what each component is. Live part is filled in by `gather`. 10 pub const COMPONENTS: &[(&str, &str, &str, &str)] = &[ 11 // key, display name, repository, role (one line, ours) 12 ("aienos", "aienux", "aien-dev/aienux", "The AienOS kernel: boot, scheduling, IPC and capability boundaries. Agent-first; the model is never the kernel."), 13 ("omega", "Omega", "aien-dev/omega", "Aien's language for all computing, GPU to CPU. Replacing CUDA is not true yet."), 14 ("sovereign-core", "Sovereign core", "aien-dev/aien-sovereign-core", "Host runtime, inference ABI, and the pipeline that judges every change."), 15 ("architecture", "Architecture", "aien-dev/aien-architecture", "Flows, boundaries, decision records, execution plan."), 16 ("protocols", "Protocols", "aien-dev/aien-protocols", "Specifications, wire protocols, schemas."), 17 ("physics", "Physics", "aien-dev/physics", "The FORGE realization layer and the boot artefacts for the GB10."), 18 ]; 19 20 #[derive(Debug, Clone, Serialize, Deserialize, Default)] 21 pub struct Merge { 22 pub number: u64, 23 pub title: String, 24 pub merged_at: String, 25 pub url: String, 26 } 27 28 #[derive(Debug, Clone, Serialize, Deserialize, Default)] 29 pub struct Component { 30 pub key: String, 31 pub name: String, 32 pub repo: String, 33 pub role: String, 34 pub url: String, 35 pub pushed_at: String, 36 pub open_prs: u64, 37 pub last_merge: Option, 38 } 39 40 #[derive(Debug, Clone, Serialize, Deserialize, Default)] 41 pub struct Decision { 42 pub id: String, 43 pub title: String, 44 pub url: String, 45 } 46 47 /// The "AIEN Verification" commit status on the latest commit of a repository's main branch: 48 /// the receipt that the Spark judge published for it. 49 #[derive(Debug, Clone, Serialize, Deserialize, Default)] 50 pub struct Receipt { 51 pub repo: String, 52 pub sha: String, 53 pub commit_title: String, 54 pub committed_at: String, 55 /// success | failure | pending | none 56 pub state: String, 57 pub description: String, 58 pub target_url: String, 59 pub updated_at: String, 60 } 61 62 #[derive(Debug, Clone, Serialize, Deserialize, Default)] 63 pub struct Facts { 64 pub generated_at: String, 65 pub components: Vec, 66 pub decisions: Vec, 67 pub receipt: Receipt, 68 } 69 70 impl Facts { 71 pub fn load(path: &Path) -> Result { 72 let text = std::fs::read_to_string(path) 73 .map_err(|e| format!("cannot read {}: {e} (run `site-builder facts {}` first)", path.display(), path.display()))?; 74 let mut facts: Facts = serde_json::from_str(&text).map_err(|e| format!("{} is not a facts file: {e}", path.display()))?; 75 facts.decisions.retain(|d| !retired_name(&d.title)); 76 Ok(facts) 77 } 78 79 pub fn save(&self, path: &Path) -> Result<(), String> { 80 let text = serde_json::to_string_pretty(self).map_err(|e| e.to_string())?; 81 std::fs::write(path, text + "\n").map_err(|e| format!("cannot write {}: {e}", path.display())) 82 } 83 } 84 85 pub(crate) fn gh(args: &[&str]) -> Result { 86 let out = Command::new("gh") 87 .arg("api") 88 .args(args) 89 .output() 90 .map_err(|e| format!("cannot run gh: {e}"))?; 91 if !out.status.success() { 92 return Err(format!( 93 "gh api {} failed: {}", 94 args.join(" "), 95 String::from_utf8_lossy(&out.stderr).trim() 96 )); 97 } 98 serde_json::from_slice(&out.stdout).map_err(|e| format!("gh api {}: bad JSON: {e}", args.join(" "))) 99 } 100 101 fn s(v: &serde_json::Value, key: &str) -> String { 102 v.get(key).and_then(|x| x.as_str()).unwrap_or("").to_string() 103 } 104 105 /// An unsourced comparison or an overclaim in a live PR title is not rendered. 106 /// An honest comparison that is dated and backed by a receipt or by the other 107 /// project's published numbers is kept. The title is not rewritten. 108 fn comparison_title(t: &str) -> bool { 109 !crate::components::machines::comparison_errors(t).is_empty() 110 } 111 112 /// The retired name is not rendered, including in an automatically collected 113 /// decision title. The record remains in the architecture repository. 114 pub(crate) fn retired_name(t: &str) -> bool { 115 t.to_ascii_lowercase().contains("aienix") 116 } 117 118 pub(crate) fn now_utc() -> String { 119 // RFC 3339 to the second, without pulling in a time crate. 120 let secs = std::time::SystemTime::now() 121 .duration_since(std::time::UNIX_EPOCH) 122 .map(|d| d.as_secs()) 123 .unwrap_or(0); 124 let days = secs / 86_400; 125 let rem = secs % 86_400; 126 let (h, m, sec) = (rem / 3600, (rem % 3600) / 60, rem % 60); 127 // civil from days (Howard Hinnant's algorithm) 128 let z = days as i64 + 719_468; 129 let era = z.div_euclid(146_097); 130 let doe = z.rem_euclid(146_097); 131 let yoe = (doe - doe / 1460 + doe / 36_524 - doe / 146_096) / 365; 132 let y = yoe + era * 400; 133 let doy = doe - (365 * yoe + yoe / 4 - yoe / 100); 134 let mp = (5 * doy + 2) / 153; 135 let d = doy - (153 * mp + 2) / 5 + 1; 136 let mo = if mp < 10 { mp + 3 } else { mp - 9 }; 137 let y = if mo <= 2 { y + 1 } else { y }; 138 format!("{y:04}-{mo:02}-{d:02}T{h:02}:{m:02}:{sec:02}Z") 139 } 140 141 /// Gather everything through `gh api`. Read-only, public data; a plain `GH_TOKEN` is enough. 142 pub fn gather() -> Result { 143 let mut components = Vec::new(); 144 for (key, name, repo, role) in COMPONENTS { 145 let meta = gh(&[&format!("repos/{repo}")])?; 146 let full = s(&meta, "full_name"); 147 let full = if full.is_empty() { repo.to_string() } else { full }; 148 let open = gh(&[&format!("search/issues?q=repo:{full}+is:pr+is:open&per_page=1")])?; 149 let open_prs = open.get("total_count").and_then(|x| x.as_u64()).unwrap_or(0); 150 let pulls = gh(&[&format!("repos/{full}/pulls?state=closed&sort=updated&direction=desc&per_page=30")])?; 151 let last_merge = pulls 152 .as_array() 153 .map(|a| { 154 a.iter() 155 .filter(|p| p.get("merged_at").map(|m| !m.is_null()).unwrap_or(false)) 156 .filter(|p| !comparison_title(&s(p, "title"))) 157 .max_by(|a, b| s(a, "merged_at").cmp(&s(b, "merged_at"))) 158 .map(|p| Merge { 159 number: p.get("number").and_then(|n| n.as_u64()).unwrap_or(0), 160 title: s(p, "title"), 161 merged_at: s(p, "merged_at"), 162 // The merge commit on the forge, or "" when the repository has no 163 // Radicle copy; GitHub's own page is never published. 164 url: match (crate::forge::repo_url(full.rsplit('/').next().unwrap_or(&full)), s(p, "merge_commit_sha")) { 165 (Some(r), sha) if !sha.is_empty() => format!("{r}/commit/{sha}"), 166 _ => String::new(), 167 }, 168 }) 169 }) 170 .unwrap_or(None); 171 components.push(Component { 172 key: key.to_string(), 173 name: name.to_string(), 174 repo: repo.to_string(), 175 role: role.to_string(), 176 url: crate::forge::repo_url(full.rsplit('/').next().unwrap_or(&full)).unwrap_or_default(), 177 pushed_at: s(&meta, "pushed_at"), 178 open_prs, 179 last_merge, 180 }); 181 } 182 183 // Decision records: the newest five by number, titles from the file names. 184 let adr = gh(&["repos/aien-dev/aien-architecture/contents/docs/adr"])?; 185 let mut decisions: Vec = adr 186 .as_array() 187 .map(|a| { 188 a.iter() 189 .filter_map(|f| { 190 let name = s(f, "name"); 191 let stem = name.strip_suffix(".md")?; 192 let (id, slug) = stem.split_once('-')?; 193 if id.len() != 4 || !id.chars().all(|c| c.is_ascii_digit()) { 194 return None; 195 } 196 let mut title = slug.replace('-', " "); 197 if let Some(first) = title.get_mut(0..1) { 198 first.make_ascii_uppercase(); 199 } 200 Some(Decision { id: id.to_string(), title, url: s(f, "html_url") }) 201 }) 202 .collect() 203 }) 204 .unwrap_or_default(); 205 decisions.sort_by(|a, b| b.id.cmp(&a.id)); 206 decisions.truncate(6); 207 // Prefer each record's own heading ("# ADR 0035: Persistent Cognitive Entity Boundary (ALLEN)"). 208 for d in &mut decisions { 209 let raw = Command::new("gh") 210 .args(["api", "-H", "Accept: application/vnd.github.raw"]) 211 .arg(format!("repos/aien-dev/aien-architecture/contents/docs/adr/{}", d.url.rsplit('/').next().unwrap_or(""))) 212 .output(); 213 if let Ok(out) = raw { 214 if out.status.success() { 215 let text = String::from_utf8_lossy(&out.stdout); 216 if let Some(h) = text.lines().find_map(|l| l.strip_prefix("# ")) { 217 let h = h.trim(); 218 let h = h.split_once(':').filter(|(a, _)| a.trim().eq_ignore_ascii_case(&format!("ADR {}", d.id))).map(|(_, t)| t.trim()).unwrap_or(h); 219 if !h.is_empty() { 220 d.title = clip(h, 72); 221 } 222 } 223 } 224 } 225 } 226 decisions.retain(|d| !retired_name(&d.title)); 227 decisions.truncate(5); 228 // The receipt: latest main commit of sovereign-core and its "AIEN Verification" status. 229 let repo = "aien-dev/aien-sovereign-core"; 230 let commit = gh(&[&format!("repos/{repo}/commits/main")])?; 231 let sha = s(&commit, "sha"); 232 let commit_title = commit 233 .pointer("/commit/message") 234 .and_then(|m| m.as_str()) 235 .and_then(|m| m.lines().next()) 236 .unwrap_or("") 237 .to_string(); 238 // An unsourced comparison or an overclaim in the commit title is not 239 // rendered. The receipt still names the commit and its link. 240 let commit_title = if comparison_title(&commit_title) { 241 "Verification receipt for this commit".to_string() 242 } else { 243 commit_title 244 }; 245 let committed_at = commit.pointer("/commit/committer/date").and_then(|d| d.as_str()).unwrap_or("").to_string(); 246 // The judge is the Spark. Its verdict is posted as the commit status whose context is 247 // "AIEN Verification (Spark)". The hosted check run that is also named "AIEN Verification" 248 // is only the bridge job and finishes in a minute whatever the Spark later finds, so it 249 // is never read here: it said success on a commit the Spark had marked FAIL. 250 let combined = gh(&[&format!("repos/{repo}/commits/{sha}/status")])?; 251 let verification = combined 252 .get("statuses") 253 .and_then(|a| a.as_array()) 254 .and_then(|a| a.iter().filter(|r| s(r, "context") == SPARK_STATUS_CONTEXT).max_by(|a, b| s(a, "updated_at").cmp(&s(b, "updated_at")))); 255 let receipt = match verification { 256 Some(status) => { 257 let state = match s(status, "state").as_str() { 258 "success" => "success", 259 "failure" | "error" => "failure", 260 _ => "pending", 261 }; 262 // The receipt is published only when it is on the forge; else the commit on the 263 // forge, else "" (the page then prints repository and commit as plain text). 264 let target_url = { 265 let t = s(status, "target_url"); 266 if t.starts_with(crate::forge::FORGE) { 267 t 268 } else { 269 crate::forge::repo_url(repo.rsplit('/').next().unwrap_or(repo)).map(|r| format!("{r}/commit/{sha}")).unwrap_or_default() 270 } 271 }; 272 Receipt { 273 repo: repo.to_string(), 274 sha, 275 commit_title, 276 committed_at, 277 state: state.to_string(), 278 description: public_description(&s(status, "description")), 279 target_url, 280 updated_at: s(status, "updated_at"), 281 } 282 } 283 None => Receipt { 284 repo: repo.to_string(), 285 sha, 286 commit_title, 287 committed_at, 288 state: "none".to_string(), 289 description: "The Spark has not published a verdict for this commit yet.".to_string(), 290 target_url: String::new(), 291 updated_at: String::new(), 292 }, 293 }; 294 295 Ok(Facts { generated_at: now_utc(), components, decisions, receipt }) 296 } 297 298 /// The commit status context the Spark judge posts its verdict under. 299 pub const SPARK_STATUS_CONTEXT: &str = "AIEN Verification (Spark)"; 300 301 /// The Spark writes its verdict line followed by the bundle path on its own disk. The 302 /// verdict is public; the path is not useful to anyone else, so it is cut off here. 303 pub fn public_description(raw: &str) -> String { 304 let cut = raw.find(": bundle ").or_else(|| raw.find(" bundle /")).unwrap_or(raw.len()); 305 raw[..cut].trim().to_string() 306 } 307 308 /// Cut a long title at a word boundary so it reads as a title, not a sentence. 309 pub fn clip(text: &str, max: usize) -> String { 310 if text.chars().count() <= max { 311 return text.to_string(); 312 } 313 let head: String = text.chars().take(max).collect(); 314 let cut = head.rfind(' ').unwrap_or(head.len()); 315 format!("{}...", head[..cut].trim_end_matches([',', ':', ';'])) 316 } 317 318 /// "2026-10-16T15:48:43Z" -> "16 Oct 2026". Anything else is returned unchanged. 319 pub fn short_date(ts: &str) -> String { 320 const MONTHS: [&str; 12] = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]; 321 let date = ts.split('T').next().unwrap_or(ts); 322 let parts: Vec<&str> = date.split('-').collect(); 323 if parts.len() != 3 { 324 return ts.to_string(); 325 } 326 let month = parts[1].parse::().ok().filter(|m| (1..=12).contains(m)); 327 let day = parts[2].trim_start_matches('0'); 328 match month { 329 Some(m) => format!("{day} {} {}", MONTHS[m - 1], parts[0]), 330 None => ts.to_string(), 331 } 332 } 333 334 #[cfg(test)] 335 mod tests { 336 use super::*; 337 338 #[test] 339 fn public_description_drops_the_bundle_path() { 340 assert_eq!( 341 public_description("BLOCKED FAIL PREFILL-E2E-2:rc_nonzero: bundle /home/x/.local/state/bundles/a/b"), 342 "BLOCKED FAIL PREFILL-E2E-2:rc_nonzero" 343 ); 344 assert_eq!(public_description("PASS 19 gates"), "PASS 19 gates"); 345 assert_eq!(public_description(""), ""); 346 } 347 348 #[test] 349 fn short_date_reads_rfc3339() { 350 assert_eq!(short_date("2026-10-16T15:48:43Z"), "16 Oct 2026"); 351 assert_eq!(short_date("2026-01-05"), "5 Jan 2026"); 352 assert_eq!(short_date("not a date"), "not a date"); 353 } 354 355 #[test] 356 fn clip_cuts_at_a_word() { 357 assert_eq!(clip("short", 72), "short"); 358 assert_eq!(clip("AIEN Verification: Check IR (manifest v2), exactness classes", 30), "AIEN Verification: Check IR..."); 359 } 360 361 #[test] 362 fn now_utc_is_rfc3339_shaped() { 363 let t = now_utc(); 364 assert_eq!(t.len(), 20, "{t}"); 365 assert!(t.ends_with('Z')); 366 assert_eq!(&t[4..5], "-"); 367 assert_eq!(&t[10..11], "T"); 368 } 369 370 #[test] 371 fn facts_round_trip() { 372 let f = Facts { generated_at: "2026-10-16T00:00:00Z".into(), ..Default::default() }; 373 let text = serde_json::to_string(&f).unwrap(); 374 let back: Facts = serde_json::from_str(&text).unwrap(); 375 assert_eq!(back.generated_at, f.generated_at); 376 } 377 }