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 / spark-debugger / src / reporter.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 aien_localhttp::Client; |
| 17 | use rusqlite::params; |
| 18 | use serde_json::json; |
| 19 | use tracing::{error, info}; |
| 20 | use uuid::Uuid; |
| 21 | |
| 22 | use crate::audit::SystemAuditReport; |
| 23 | |
| 24 | const CORTEX_URL: &str = "http://127.0.0.1:18080"; |
| 25 | fn debugger_cortex_token_path() -> std::path::PathBuf { |
| 26 | let home = std::env::var("HOME") |
| 27 | .or_else(|_| std::env::var("USERPROFILE")) |
| 28 | .map(std::path::PathBuf::from) |
| 29 | .unwrap_or_else(|_| std::path::PathBuf::from(".")); |
| 30 | home.join(".config/cortex/token") |
| 31 | } |
| 32 | |
| 33 | fn debugger_hive_db_path() -> std::path::PathBuf { |
| 34 | let home = std::env::var("HOME") |
| 35 | .or_else(|_| std::env::var("USERPROFILE")) |
| 36 | .map(std::path::PathBuf::from) |
| 37 | .unwrap_or_else(|_| std::path::PathBuf::from(".")); |
| 38 | home.join(".config/cortex/hive.db") |
| 39 | } |
| 40 | |
| 41 | /// POSTs the bug report to `{cortex_url}/api/cortex/write`. Only a transport |
| 42 | /// failure is an error; the response status is not checked (unchanged). |
| 43 | async fn post_bug_report( |
| 44 | client: &Client, |
| 45 | cortex_url: &str, |
| 46 | token: &str, |
| 47 | payload: &serde_json::Value, |
| 48 | ) -> Result<(), aien_localhttp::Error> { |
| 49 | client |
| 50 | .post(format!("{}/api/cortex/write", cortex_url)) |
| 51 | .header("Authorization", format!("Bearer {}", token.trim())) |
| 52 | .json(payload) |
| 53 | .send() |
| 54 | .await |
| 55 | .map(|_| ()) |
| 56 | } |
| 57 | |
| 58 | pub async fn report_incident(client: &Client, report: &SystemAuditReport, failure_summary: &str) { |
| 59 | info!("[DebuggerReporter] Reporting incident: {}", failure_summary); |
| 60 | |
| 61 | // 1. Commit bug report to Cortex memory |
| 62 | if let Ok(token) = std::fs::read_to_string(debugger_cortex_token_path()) { |
| 63 | let payload = json!({ |
| 64 | "space": "atlas-memory", |
| 65 | "canonicalName": format!("incident-{}", Uuid::new_v4().to_string()[..8].to_string()), |
| 66 | "entityType": "bug_report", |
| 67 | "content": format!("Incident detected by spark-debugger: {} |
| 68 | Report Details: {:?}", failure_summary, report) |
| 69 | }); |
| 70 | |
| 71 | if let Err(e) = post_bug_report(client, CORTEX_URL, &token, &payload).await { |
| 72 | error!( |
| 73 | "[DebuggerReporter] Failed to commit bug report to Cortex: {}", |
| 74 | e |
| 75 | ); |
| 76 | } else { |
| 77 | info!("[DebuggerReporter] Committed bug report to Cortex."); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | // 2. Emit alert comb onto Honeycomb lattice if hive.db exists |
| 82 | if debugger_hive_db_path().exists() { |
| 83 | if let Ok(conn) = rusqlite::Connection::open(debugger_hive_db_path()) { |
| 84 | let comb_id = format!("comb-alert-{}", &Uuid::new_v4().to_string()[..8]); |
| 85 | let now = chrono::Utc::now().to_rfc3339(); |
| 86 | let content = format!("🚨 AUDIT ALERT: {}", failure_summary); |
| 87 | |
| 88 | // Find free coordinate or default to outer ring |
| 89 | let res = conn.execute( |
| 90 | "INSERT INTO hive_combs (id, q, r, author, role, content, intent, created_at) |
| 91 | VALUES (?1, -2, 2, ?2, ?3, ?4, ?5, ?6)", |
| 92 | params![ |
| 93 | comb_id, |
| 94 | "spark-debugger", |
| 95 | "watchdog", |
| 96 | content, |
| 97 | "branch", |
| 98 | now |
| 99 | ], |
| 100 | ); |
| 101 | if res.is_ok() { |
| 102 | info!("[DebuggerReporter] Emitted alert comb to Honeycomb Wall."); |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | #[cfg(test)] |
| 109 | mod tests { |
| 110 | use super::*; |
| 111 | use aien_localhttp::test_server::{closed_port, response, serve_once}; |
| 112 | |
| 113 | #[test] |
| 114 | fn cortex_url_is_plain_http() { |
| 115 | assert!(aien_localhttp::Url::parse(CORTEX_URL).is_ok()); |
| 116 | } |
| 117 | |
| 118 | #[tokio::test] |
| 119 | async fn post_bug_report_sends_the_same_request() { |
| 120 | let (base, server) = serve_once(response("200 OK", "application/json", "{}")).await; |
| 121 | let payload = json!({"space": "atlas-memory", "entityType": "bug_report"}); |
| 122 | post_bug_report(&Client::new(), &base, " tok\n", &payload) |
| 123 | .await |
| 124 | .unwrap(); |
| 125 | let request = server.await.unwrap(); |
| 126 | assert!( |
| 127 | request.starts_with("POST /api/cortex/write HTTP/1.1\r\n"), |
| 128 | "{request}" |
| 129 | ); |
| 130 | assert!( |
| 131 | request.contains("\r\nAuthorization: Bearer tok\r\n"), |
| 132 | "{request}" |
| 133 | ); |
| 134 | assert!(request.contains("\r\nContent-Type: application/json\r\n")); |
| 135 | let body: serde_json::Value = |
| 136 | serde_json::from_str(request.split("\r\n\r\n").nth(1).unwrap()).unwrap(); |
| 137 | assert_eq!(body, payload); |
| 138 | } |
| 139 | |
| 140 | #[tokio::test] |
| 141 | async fn post_bug_report_ignores_status_but_reports_transport_failures() { |
| 142 | let (base, _server) = |
| 143 | serve_once(response("500 Internal Server Error", "text/plain", "")).await; |
| 144 | assert!(post_bug_report(&Client::new(), &base, "t", &json!({})) |
| 145 | .await |
| 146 | .is_ok()); |
| 147 | |
| 148 | let err = post_bug_report(&Client::new(), &closed_port().await, "t", &json!({})) |
| 149 | .await |
| 150 | .unwrap_err(); |
| 151 | assert!(err.is_connect(), "{err:?}"); |
| 152 | |
| 153 | let err = post_bug_report(&Client::new(), "https://127.0.0.1:1", "t", &json!({})) |
| 154 | .await |
| 155 | .unwrap_err(); |
| 156 | assert!( |
| 157 | matches!(err, aien_localhttp::Error::HttpsRefused { .. }), |
| 158 | "{err:?}" |
| 159 | ); |
| 160 | } |
| 161 | } |