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 / aien-cli / src / nesting.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 chrono::Utc; |
| 17 | use colored::*; |
| 18 | use reqwest::Client; |
| 19 | use serde_json::json; |
| 20 | use std::fs::{self, OpenOptions}; |
| 21 | use std::io::Write; |
| 22 | use std::process::Command; |
| 23 | |
| 24 | use crate::crumbs::{ |
| 25 | agent_identity, discover_workspace_crumbs_summary, load_dir_crumb_view, record_as_self, |
| 26 | }; |
| 27 | use crate::hive::hive_roster; |
| 28 | use crate::platform::PlatformContext; |
| 29 | use crate::telemetry::get_gpu_telemetry; |
| 30 | |
| 31 | #[allow(dead_code)] |
| 32 | pub struct NestingReport { |
| 33 | pub hostname: String, |
| 34 | pub user: String, |
| 35 | pub gpu_info: String, |
| 36 | pub disk_free: String, |
| 37 | pub seats_status: Vec<(String, bool)>, |
| 38 | pub hive_active_count: usize, |
| 39 | pub pending_parks_count: usize, |
| 40 | pub scent_marked: bool, |
| 41 | pub grounding_context: String, |
| 42 | } |
| 43 | |
| 44 | pub async fn perform_nesting_ritual(session_id: &str, print_tui: bool) -> NestingReport { |
| 45 | let client = Client::builder() |
| 46 | .timeout(std::time::Duration::from_secs(2)) |
| 47 | .build() |
| 48 | .unwrap_or_default(); |
| 49 | |
| 50 | let platform = PlatformContext::detect(); |
| 51 | let hostname = platform.hostname.clone(); |
| 52 | let user = platform.current_user.clone(); |
| 53 | |
| 54 | // 1. Circle & Survey Surroundings (Hardware, user, disk, seats) |
| 55 | let gpu = get_gpu_telemetry(); |
| 56 | let gpu_info = if gpu.available { |
| 57 | format!( |
| 58 | "{} ({}, {}, {} util)", |
| 59 | gpu.display_name(), |
| 60 | gpu.display_temp(), |
| 61 | gpu.display_vram(), |
| 62 | gpu.display_util() |
| 63 | ) |
| 64 | } else { |
| 65 | "Unavailable / Sensor Offline (CPU Execution Mode)".to_string() |
| 66 | }; |
| 67 | |
| 68 | let home_str = platform.home_dir.to_string_lossy().to_string(); |
| 69 | let disk_free = Command::new("df") |
| 70 | .args(["-h", &home_str]) |
| 71 | .output() |
| 72 | .map(|o| { |
| 73 | let s = String::from_utf8_lossy(&o.stdout); |
| 74 | s.lines() |
| 75 | .nth(1) |
| 76 | .and_then(|l| l.split_whitespace().nth(3)) |
| 77 | .unwrap_or("unknown") |
| 78 | .to_string() |
| 79 | }) |
| 80 | .unwrap_or_else(|_| "unknown".to_string()); |
| 81 | |
| 82 | // Check service seats |
| 83 | let mut seats_status = Vec::new(); |
| 84 | let seat_18006 = client |
| 85 | .get("http://127.0.0.1:18006/v1/models") |
| 86 | .send() |
| 87 | .await |
| 88 | .map(|r| r.status().is_success()) |
| 89 | .unwrap_or(false); |
| 90 | seats_status.push(("Model (18006)".to_string(), seat_18006)); |
| 91 | |
| 92 | let judge_18082 = client |
| 93 | .get("http://127.0.0.1:18082/v1/models") |
| 94 | .send() |
| 95 | .await |
| 96 | .map(|r| r.status().is_success()) |
| 97 | .unwrap_or(false); |
| 98 | seats_status.push(("Judge (18082)".to_string(), judge_18082)); |
| 99 | |
| 100 | let cortex_18080 = client |
| 101 | .get("http://127.0.0.1:18080/health") |
| 102 | .send() |
| 103 | .await |
| 104 | .map(|r| r.status().is_success()) |
| 105 | .unwrap_or(false); |
| 106 | seats_status.push(("Cortex (18080)".to_string(), cortex_18080)); |
| 107 | |
| 108 | let dream_18085 = client |
| 109 | .get("http://127.0.0.1:18085/health") |
| 110 | .send() |
| 111 | .await |
| 112 | .map(|r| r.status().is_success()) |
| 113 | .unwrap_or(false); |
| 114 | seats_status.push(("Dream (18085)".to_string(), dream_18085)); |
| 115 | |
| 116 | // Environmental verification: if running under dedicated DGX Spark profile, enforce strict anchors |
| 117 | if platform.is_dgx_spark() { |
| 118 | let mut critical_failures = Vec::new(); |
| 119 | if user != "drakestapleton" && user != "root" { |
| 120 | critical_failures.push(format!("DGX Spark workstation policy: running as {}, must operate as drakestapleton on spark hardware", user)); |
| 121 | } |
| 122 | if !seat_18006 { |
| 123 | critical_failures.push( |
| 124 | "Critical Seat Down: Model Seat (port 18006 / Nemotron-3.5) is offline".to_string(), |
| 125 | ); |
| 126 | } |
| 127 | if !judge_18082 { |
| 128 | critical_failures.push( |
| 129 | "Critical Seat Down: JSpace Truth Judge (port 18082 / Llama-3.2) is offline" |
| 130 | .to_string(), |
| 131 | ); |
| 132 | } |
| 133 | if !cortex_18080 { |
| 134 | critical_failures.push( |
| 135 | "Critical Seat Down: Spark Cortex Memory (port 18080) is offline".to_string(), |
| 136 | ); |
| 137 | } |
| 138 | |
| 139 | if !critical_failures.is_empty() { |
| 140 | eprintln!( |
| 141 | "\n{}", |
| 142 | "❌ NESTING RITUAL FAILED: CRITICAL HARD-BLOCK TRIGGERED" |
| 143 | .red() |
| 144 | .bold() |
| 145 | ); |
| 146 | eprintln!("{}", "The agent cannot settle or take ungrounded actions because required environmental anchors are down:".red()); |
| 147 | for err in &critical_failures { |
| 148 | eprintln!(" ✖ {}", err.bold().red()); |
| 149 | } |
| 150 | eprintln!( |
| 151 | "\n{}", |
| 152 | "Aborting startup to prevent ungrounded or misdirected execution.".yellow() |
| 153 | ); |
| 154 | std::process::exit(1); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | // 2. Check the Wind (Hive swarm activity, pulse, vibe & parked notes) |
| 159 | let pulse_path = platform.home_dir.join("basecamp").join("hive-pulse.json"); |
| 160 | let (pulse_coherence, pulse_vibe, _pulse_status) = if pulse_path.exists() { |
| 161 | if let Ok(c) = fs::read_to_string(&pulse_path) { |
| 162 | if let Ok(v) = serde_json::from_str::<serde_json::Value>(&c) { |
| 163 | let coh = v |
| 164 | .get("coherence_score") |
| 165 | .and_then(serde_json::Value::as_u64) |
| 166 | .unwrap_or(95); |
| 167 | let vibe = v |
| 168 | .get("vibe_summary") |
| 169 | .and_then(serde_json::Value::as_str) |
| 170 | .unwrap_or("Harmonious forward vector") |
| 171 | .to_string(); |
| 172 | let st = v |
| 173 | .get("pulse_status") |
| 174 | .and_then(serde_json::Value::as_str) |
| 175 | .unwrap_or("harmonious") |
| 176 | .to_string(); |
| 177 | (Some(coh), Some(vibe), Some(st)) |
| 178 | } else { |
| 179 | (None, None, None) |
| 180 | } |
| 181 | } else { |
| 182 | (None, None, None) |
| 183 | } |
| 184 | } else { |
| 185 | (None, None, None) |
| 186 | }; |
| 187 | |
| 188 | let roster_text = hive_roster(); |
| 189 | let hive_active_count = if roster_text.contains("idle") |
| 190 | || roster_text.contains("Empty") |
| 191 | || roster_text.contains("0") |
| 192 | { |
| 193 | 0 |
| 194 | } else { |
| 195 | roster_text |
| 196 | .lines() |
| 197 | .filter(|l| l.contains("running") || l.contains("busy")) |
| 198 | .count() |
| 199 | }; |
| 200 | |
| 201 | let park_dir = platform.home_dir.join("atlas-prime-workspace").join("park"); |
| 202 | let pending_parks_count = if park_dir.exists() { |
| 203 | fs::read_dir(&park_dir).map(|r| r.count()).unwrap_or(0) |
| 204 | } else { |
| 205 | 0 |
| 206 | }; |
| 207 | |
| 208 | // 3. Hierarchical Directory Crumb Inspection & Scent-Drop |
| 209 | let cwd = std::env::current_dir().unwrap_or_else(|_| platform.home_dir.clone()); |
| 210 | let (dir_crumb, local_err) = load_dir_crumb_view(&cwd); |
| 211 | let crumb_topography = discover_workspace_crumbs_summary(&cwd); |
| 212 | |
| 213 | // Record presence into directory crumb (advisory notes, core#924: a |
| 214 | // failure is shown, never reported as signed in). |
| 215 | let signin = record_as_self( |
| 216 | &cwd, |
| 217 | "signin", |
| 218 | "Operator session initiation", |
| 219 | "Awaiting operator command", |
| 220 | ); |
| 221 | let actor = agent_identity() |
| 222 | .map(|id| id.agent_id) |
| 223 | .unwrap_or_else(|e| format!("unidentified ({e})")); |
| 224 | |
| 225 | // 4. Leave Scent (Record presence in basecamp/events.jsonl) |
| 226 | let events_path = platform.home_dir.join("basecamp").join("events.jsonl"); |
| 227 | let mut scent_marked = false; |
| 228 | let event_entry = json!({ |
| 229 | "event": "agent_nesting", |
| 230 | "timestamp": Utc::now().to_rfc3339(), |
| 231 | "session_id": session_id, |
| 232 | "actor": actor, |
| 233 | "mask": "AIEN", |
| 234 | "user": user, |
| 235 | "host": hostname, |
| 236 | "cwd": cwd.to_string_lossy().to_string(), |
| 237 | "status": "grounded", |
| 238 | "gpu_available": gpu.available, |
| 239 | "gpu_temp_c": gpu.temp_c, |
| 240 | "disk_free": disk_free |
| 241 | }); |
| 242 | |
| 243 | if let Ok(mut f) = OpenOptions::new() |
| 244 | .create(true) |
| 245 | .append(true) |
| 246 | .open(&events_path) |
| 247 | { |
| 248 | if writeln!(f, "{}", event_entry).is_ok() { |
| 249 | scent_marked = true; |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | // 5. Flatten the Terrain (Build Context & Render TUI) |
| 254 | let grounding_context = format!( |
| 255 | "[NESTING RITUAL RECORD - GROUNDING SEQUENCE]\n\ |
| 256 | Host: {} | Operator: {} | Disk Free: {}\n\ |
| 257 | GPU: {}\n\ |
| 258 | Seats Verified: Model(18006)={}, Judge(18082)={}, Cortex(18080)={}, Dream(18085)={}\n\ |
| 259 | Swarm Status: {} active peer cell(s) | Pending Park Notes: {}\n\ |
| 260 | Hive Pulse & Vibe: Coherence={}%, Vibe={}\n\ |
| 261 | Scent Registered: basecamp/events.jsonl ({})\n\n\ |
| 262 | {}\n\n\ |
| 263 | Operating Rule: Consult the directory crumb topography above. Before modifying files, declare your vector and verify boundaries.", |
| 264 | hostname, user, disk_free, gpu_info, |
| 265 | seat_18006, judge_18082, cortex_18080, dream_18085, |
| 266 | hive_active_count, pending_parks_count, |
| 267 | pulse_coherence.unwrap_or(95), pulse_vibe.as_deref().unwrap_or("Aligned and focused"), |
| 268 | session_id, crumb_topography |
| 269 | ); |
| 270 | |
| 271 | if print_tui { |
| 272 | println!( |
| 273 | "{}", |
| 274 | "╭─────────────────────────────────────────────────────────────╮".cyan() |
| 275 | ); |
| 276 | println!( |
| 277 | "│ {} │", |
| 278 | "🐕 NESTING RITUAL (Agent Grounding Initiation)" |
| 279 | .bold() |
| 280 | .yellow() |
| 281 | ); |
| 282 | println!( |
| 283 | "{}", |
| 284 | "├─────────────────────────────────────────────────────────────┤".cyan() |
| 285 | ); |
| 286 | println!( |
| 287 | "│ {} {} ({})", |
| 288 | "Circle Terrain:".bold().white(), |
| 289 | hostname.green(), |
| 290 | user.cyan() |
| 291 | ); |
| 292 | println!("│ ├─ Storage Free: {} available", disk_free.green()); |
| 293 | if gpu.available { |
| 294 | println!( |
| 295 | "│ ├─ GPU Telemetry: {} | {} | {}", |
| 296 | gpu.display_name(), |
| 297 | gpu.display_temp(), |
| 298 | gpu.display_vram() |
| 299 | ); |
| 300 | } else { |
| 301 | println!( |
| 302 | "│ ├─ GPU Telemetry: {}", |
| 303 | "Unavailable / Sensor Offline (CPU Mode)".yellow() |
| 304 | ); |
| 305 | } |
| 306 | print!("│ └─ Seats Online: "); |
| 307 | for (name, ok) in &seats_status { |
| 308 | if *ok { |
| 309 | print!("{}✓ ", name.green()); |
| 310 | } else { |
| 311 | print!("{}✗ ", name.red()); |
| 312 | } |
| 313 | } |
| 314 | println!(); |
| 315 | println!( |
| 316 | "│ {} {} active hive cell(s) | {} park note(s)", |
| 317 | "Check the Wind:".bold().white(), |
| 318 | hive_active_count, |
| 319 | pending_parks_count |
| 320 | ); |
| 321 | if let (Some(coh), Some(vibe)) = (&pulse_coherence, &pulse_vibe) { |
| 322 | println!( |
| 323 | "│ ├─ Hive Pulse: {} (Coherence: {}%)", |
| 324 | "💚 Heartbeat Active".bold().green(), |
| 325 | coh |
| 326 | ); |
| 327 | println!("│ ├─ Hive Vibe: {}", vibe.italic().magenta()); |
| 328 | } |
| 329 | |
| 330 | let above_str = dir_crumb |
| 331 | .above |
| 332 | .as_ref() |
| 333 | .map(|a| a.name.clone()) |
| 334 | .unwrap_or_else(|| "Root".to_string()); |
| 335 | let subdirs: Vec<&str> = dir_crumb |
| 336 | .below |
| 337 | .iter() |
| 338 | .filter(|i| i.item_type == "dir") |
| 339 | .map(|i| i.name.as_str()) |
| 340 | .collect(); |
| 341 | let subdirs_str = if subdirs.is_empty() { |
| 342 | "none".to_string() |
| 343 | } else { |
| 344 | subdirs.join(", ") |
| 345 | }; |
| 346 | |
| 347 | println!( |
| 348 | "│ {} Directory: {} (.crumb)", |
| 349 | "Directory Crumb:".bold().white(), |
| 350 | dir_crumb.dir_name.cyan() |
| 351 | ); |
| 352 | if let Some(p) = &dir_crumb.purpose { |
| 353 | println!("│ ├─ Purpose: {}", p.statement.italic().cyan()); |
| 354 | } |
| 355 | println!("│ ├─ Above: {}", above_str.yellow()); |
| 356 | println!("│ ├─ Below: [{}]", subdirs_str.white()); |
| 357 | match &local_err { |
| 358 | None => println!( |
| 359 | "│ └─ Local History: {} recorded action(s)", |
| 360 | dir_crumb.history.len() |
| 361 | ), |
| 362 | Some(err) => println!( |
| 363 | "│ └─ Local History: {}", |
| 364 | crate::crumbs::local_state_error_line(err).red() |
| 365 | ), |
| 366 | } |
| 367 | |
| 368 | match &signin { |
| 369 | Ok(()) => println!( |
| 370 | "│ {} Presence signed into directory .crumb.local & basecamp", |
| 371 | "Leave Scent:".bold().white() |
| 372 | ), |
| 373 | Err(err) => println!( |
| 374 | "│ {} Presence NOT signed into .crumb.local: {}", |
| 375 | "Leave Scent:".bold().white(), |
| 376 | err.red() |
| 377 | ), |
| 378 | } |
| 379 | println!( |
| 380 | "│ {} Workspace anchored, topography loaded, vector set.", |
| 381 | "Flatten Terrain:".bold().white() |
| 382 | ); |
| 383 | println!( |
| 384 | "{}", |
| 385 | "╰─────────────────────────────────────────────────────────────╯".cyan() |
| 386 | ); |
| 387 | println!( |
| 388 | "{}", |
| 389 | "✓ Grounding complete. Directory crumb topography active. Ready.\n".dimmed() |
| 390 | ); |
| 391 | } |
| 392 | |
| 393 | NestingReport { |
| 394 | hostname, |
| 395 | user, |
| 396 | gpu_info, |
| 397 | disk_free, |
| 398 | seats_status, |
| 399 | hive_active_count, |
| 400 | pending_parks_count, |
| 401 | scent_marked, |
| 402 | grounding_context, |
| 403 | } |
| 404 | } |