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-harvester / src / main.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 clap::{Parser, Subcommand}; |
| 17 | use p256::pkcs8::DecodePublicKey; |
| 18 | use std::path::PathBuf; |
| 19 | use tracing::{info, Level}; |
| 20 | use tracing_subscriber::FmtSubscriber; |
| 21 | |
| 22 | use aien_provenance::SourceGrant; |
| 23 | use spark_harvester::{BundleKind, ExtractedPair, ReasoningExtractor, RightsGate}; |
| 24 | |
| 25 | #[derive(Parser)] |
| 26 | #[command(name = "spark-harvester")] |
| 27 | #[command( |
| 28 | about = "Native Rust rights-gated harvester and provenance engine for sovereign AI datasets" |
| 29 | )] |
| 30 | struct Cli { |
| 31 | #[command(subcommand)] |
| 32 | command: Commands, |
| 33 | } |
| 34 | |
| 35 | #[derive(Subcommand)] |
| 36 | enum Commands { |
| 37 | /// Extract reasoning pairs from raw text with mandatory SourceGrant rights gating |
| 38 | Extract { |
| 39 | #[arg(short, long)] |
| 40 | prompt: String, |
| 41 | #[arg(short, long)] |
| 42 | input: String, |
| 43 | #[arg(long, default_value = "commercial-lab")] |
| 44 | provider: String, |
| 45 | #[arg(long, default_value = "frontier-model")] |
| 46 | model: String, |
| 47 | /// Path to JSON file containing a valid, signed SourceGrant |
| 48 | #[arg(long)] |
| 49 | grant_file: Option<PathBuf>, |
| 50 | /// Inline JSON string of SourceGrant |
| 51 | #[arg(long)] |
| 52 | grant_json: Option<String>, |
| 53 | /// Authority verifying key (hex-encoded SEC1 or PEM) |
| 54 | #[arg(long)] |
| 55 | authority_key: Option<String>, |
| 56 | /// Path to PEM or SEC1 file containing authority verifying key |
| 57 | #[arg(long)] |
| 58 | authority_key_file: Option<PathBuf>, |
| 59 | /// Optional output path for gated bundle |
| 60 | #[arg(short, long)] |
| 61 | output: Option<PathBuf>, |
| 62 | /// Bundle kind: training, distillation, or evidence |
| 63 | #[arg(long, default_value = "distillation")] |
| 64 | bundle_kind: String, |
| 65 | }, |
| 66 | /// Export extracted training pairs into JSONL with mandatory SourceGrant rights gating |
| 67 | Distill { |
| 68 | #[arg(short, long)] |
| 69 | output: PathBuf, |
| 70 | /// Path to JSON file containing a valid, signed SourceGrant |
| 71 | #[arg(long)] |
| 72 | grant_file: Option<PathBuf>, |
| 73 | /// Inline JSON string of SourceGrant |
| 74 | #[arg(long)] |
| 75 | grant_json: Option<String>, |
| 76 | /// Authority verifying key (hex-encoded SEC1 or PEM) |
| 77 | #[arg(long)] |
| 78 | authority_key: Option<String>, |
| 79 | /// Path to PEM or SEC1 file containing authority verifying key |
| 80 | #[arg(long)] |
| 81 | authority_key_file: Option<PathBuf>, |
| 82 | }, |
| 83 | /// Display pipeline status |
| 84 | Status, |
| 85 | } |
| 86 | |
| 87 | fn load_grant( |
| 88 | grant_file: Option<&PathBuf>, |
| 89 | grant_json: Option<&str>, |
| 90 | ) -> Result<SourceGrant, Box<dyn std::error::Error>> { |
| 91 | if let Some(path) = grant_file { |
| 92 | let content = std::fs::read_to_string(path)?; |
| 93 | let grant: SourceGrant = serde_json::from_str(&content)?; |
| 94 | Ok(grant) |
| 95 | } else if let Some(json) = grant_json { |
| 96 | let grant: SourceGrant = serde_json::from_str(json)?; |
| 97 | Ok(grant) |
| 98 | } else { |
| 99 | Err("SourceGrant required: Pass --grant-file <PATH> or --grant-json <JSON> to prove rights admission. Unlicensed extraction is strictly rejected.".into()) |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | fn load_authority_key( |
| 104 | authority_key: Option<&str>, |
| 105 | authority_key_file: Option<&PathBuf>, |
| 106 | ) -> Result<p256::ecdsa::VerifyingKey, Box<dyn std::error::Error>> { |
| 107 | if let Some(path) = authority_key_file { |
| 108 | let content = std::fs::read_to_string(path)?; |
| 109 | let trimmed = content.trim(); |
| 110 | if trimmed.starts_with("-----BEGIN") { |
| 111 | let vk = p256::ecdsa::VerifyingKey::from_public_key_pem(trimmed)?; |
| 112 | return Ok(vk); |
| 113 | } |
| 114 | if let Ok(bytes) = hex::decode(trimmed) { |
| 115 | let vk = p256::ecdsa::VerifyingKey::from_sec1_bytes(&bytes)?; |
| 116 | return Ok(vk); |
| 117 | } |
| 118 | let raw = std::fs::read(path)?; |
| 119 | let vk = p256::ecdsa::VerifyingKey::from_sec1_bytes(&raw)?; |
| 120 | return Ok(vk); |
| 121 | } |
| 122 | if let Some(k) = authority_key { |
| 123 | let trimmed = k.trim(); |
| 124 | if trimmed.starts_with("-----BEGIN") { |
| 125 | let vk = p256::ecdsa::VerifyingKey::from_public_key_pem(trimmed)?; |
| 126 | return Ok(vk); |
| 127 | } |
| 128 | let bytes = hex::decode(trimmed)?; |
| 129 | let vk = p256::ecdsa::VerifyingKey::from_sec1_bytes(&bytes)?; |
| 130 | return Ok(vk); |
| 131 | } |
| 132 | if let Ok(vault_val) = |
| 133 | spark_harvester::HarvesterClient::resolve_vault_key("ATLAS_AUTHORITY_PUBKEY") |
| 134 | { |
| 135 | let trimmed = vault_val.trim(); |
| 136 | if trimmed.starts_with("-----BEGIN") { |
| 137 | let vk = p256::ecdsa::VerifyingKey::from_public_key_pem(trimmed)?; |
| 138 | return Ok(vk); |
| 139 | } |
| 140 | if let Ok(bytes) = hex::decode(trimmed) { |
| 141 | let vk = p256::ecdsa::VerifyingKey::from_sec1_bytes(&bytes)?; |
| 142 | return Ok(vk); |
| 143 | } |
| 144 | } |
| 145 | Err("Authority verifying key required: pass --authority-key <HEX_OR_PEM>, --authority-key-file <PATH>, or register ATLAS_AUTHORITY_PUBKEY in atlas-vault.".into()) |
| 146 | } |
| 147 | |
| 148 | fn parse_bundle_kind(s: &str) -> Result<BundleKind, String> { |
| 149 | match s.to_lowercase().as_str() { |
| 150 | "training" => Ok(BundleKind::Training), |
| 151 | "distillation" => Ok(BundleKind::Distillation), |
| 152 | "evidence" => Ok(BundleKind::Evidence), |
| 153 | other => Err(format!( |
| 154 | "Unknown bundle kind {}. Must be training, distillation, or evidence", |
| 155 | other |
| 156 | )), |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | const DEFAULT_ALLOWED_LICENSES: &[&str] = &[ |
| 161 | "Apache-2.0", |
| 162 | "MIT", |
| 163 | "BSD-3-Clause", |
| 164 | "SRCL-1.0", |
| 165 | "PolyForm-Noncommercial-1.0.0", |
| 166 | ]; |
| 167 | |
| 168 | #[tokio::main] |
| 169 | async fn main() -> Result<(), Box<dyn std::error::Error>> { |
| 170 | let subscriber = FmtSubscriber::builder() |
| 171 | .with_max_level(Level::INFO) |
| 172 | .finish(); |
| 173 | tracing::subscriber::set_global_default(subscriber)?; |
| 174 | |
| 175 | let cli = Cli::parse(); |
| 176 | match cli.command { |
| 177 | Commands::Extract { |
| 178 | prompt, |
| 179 | input, |
| 180 | provider, |
| 181 | model, |
| 182 | grant_file, |
| 183 | grant_json, |
| 184 | authority_key, |
| 185 | authority_key_file, |
| 186 | output, |
| 187 | bundle_kind, |
| 188 | } => { |
| 189 | let grant = load_grant(grant_file.as_ref(), grant_json.as_deref())?; |
| 190 | let authority_vk = |
| 191 | load_authority_key(authority_key.as_deref(), authority_key_file.as_ref())?; |
| 192 | let kind = parse_bundle_kind(&bundle_kind)?; |
| 193 | |
| 194 | let extractor = ReasoningExtractor::new(); |
| 195 | let pair = extractor.extract(&prompt, &input, &provider, &model); |
| 196 | info!( |
| 197 | "Extracted pair: tokens={}, quality={:.2}", |
| 198 | pair.token_count, pair.quality_score |
| 199 | ); |
| 200 | |
| 201 | let bundle = RightsGate::validate_and_bundle( |
| 202 | &grant, |
| 203 | &[pair], |
| 204 | kind, |
| 205 | DEFAULT_ALLOWED_LICENSES, |
| 206 | &authority_vk, |
| 207 | )?; |
| 208 | info!( |
| 209 | "RightsGate accepted bundle: ID={}, MerkleRoot={:?}", |
| 210 | bundle.bundle_id, bundle.merkle_root |
| 211 | ); |
| 212 | |
| 213 | if let Some(out_path) = output { |
| 214 | let count = RightsGate::export_bundle(&bundle, &out_path)?; |
| 215 | info!("Exported {} rights-gated records to {:?}", count, out_path); |
| 216 | } |
| 217 | |
| 218 | println!("{}", serde_json::to_string_pretty(&bundle)?); |
| 219 | } |
| 220 | Commands::Distill { |
| 221 | output, |
| 222 | grant_file, |
| 223 | grant_json, |
| 224 | authority_key, |
| 225 | authority_key_file, |
| 226 | } => { |
| 227 | let grant = load_grant(grant_file.as_ref(), grant_json.as_deref())?; |
| 228 | let authority_vk = |
| 229 | load_authority_key(authority_key.as_deref(), authority_key_file.as_ref())?; |
| 230 | |
| 231 | info!("Preparing distillation with SourceGrant {}", grant.grant_id); |
| 232 | let sample_pair = ExtractedPair { |
| 233 | prompt: "Sovereign AI foundational premise".to_string(), |
| 234 | reasoning: Some("Verify local computation vs centralized extraction".to_string()), |
| 235 | completion: "Human freedom requires local ownership of intelligence.".to_string(), |
| 236 | provider: "community".to_string(), |
| 237 | model: "atlas-sovereign".to_string(), |
| 238 | token_count: 42, |
| 239 | quality_score: 1.0, |
| 240 | }; |
| 241 | |
| 242 | let bundle = RightsGate::validate_and_bundle( |
| 243 | &grant, |
| 244 | &[sample_pair], |
| 245 | BundleKind::Distillation, |
| 246 | DEFAULT_ALLOWED_LICENSES, |
| 247 | &authority_vk, |
| 248 | )?; |
| 249 | |
| 250 | let count = RightsGate::export_bundle(&bundle, &output)?; |
| 251 | info!( |
| 252 | "Appended {} verified rights-gated pairs to {:?}", |
| 253 | count, output |
| 254 | ); |
| 255 | } |
| 256 | Commands::Status => { |
| 257 | println!("AIEN Sovereign Harvester Pipeline Active"); |
| 258 | println!("Rights Enforcement: Active (Strict SourceGrant validation & structural evidence quarantine)"); |
| 259 | println!("Zero Disk Secrets: Enforced via hardware TPM vault (atlas-vault)"); |
| 260 | println!("License: PolyForm Noncommercial 1.0.0 with Sovereign Defense Covenant"); |
| 261 | } |
| 262 | } |
| 263 | Ok(()) |
| 264 | } |