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 / tests / rights_gating_tests.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_provenance::{GrantPermissions, ProvenanceError, SourceGrant}; |
| 17 | use p256::ecdsa::SigningKey; |
| 18 | use spark_harvester::{BundleKind, ExtractedPair, RightsGate}; |
| 19 | use std::io::ErrorKind; |
| 20 | |
| 21 | fn create_test_pair() -> ExtractedPair { |
| 22 | ExtractedPair { |
| 23 | prompt: "Explain copy-on-write paged attention".to_string(), |
| 24 | reasoning: Some("Analyze memory refcounting and block sharing mechanics".to_string()), |
| 25 | completion: |
| 26 | "CoW allocates physical pages only when divergent tokens are written to a shared block." |
| 27 | .to_string(), |
| 28 | provider: "sovereign-lab".to_string(), |
| 29 | model: "atlas-omni-reasoning".to_string(), |
| 30 | token_count: 32, |
| 31 | quality_score: 0.98, |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | fn create_authority_keys() -> (SigningKey, p256::ecdsa::VerifyingKey) { |
| 36 | let signing_key = SigningKey::from_slice(&[42u8; 32]).expect("valid signing key"); |
| 37 | let verifying_key = *signing_key.verifying_key(); |
| 38 | (signing_key, verifying_key) |
| 39 | } |
| 40 | |
| 41 | #[test] |
| 42 | fn test_rights_gated_training_bundle_admitted_with_valid_grant() { |
| 43 | let (sk, vk) = create_authority_keys(); |
| 44 | let mut grant = SourceGrant::new( |
| 45 | "https://hf.co/datasets/aien/sovereign-math".to_string(), |
| 46 | "Apache-2.0".to_string(), |
| 47 | GrantPermissions::TRAIN | GrantPermissions::DISTILL, |
| 48 | "Atlas Rights Authority".to_string(), |
| 49 | ); |
| 50 | grant.sign(&sk); |
| 51 | |
| 52 | let pairs = vec![create_test_pair()]; |
| 53 | let bundle = RightsGate::validate_and_bundle( |
| 54 | &grant, |
| 55 | &pairs, |
| 56 | BundleKind::Training, |
| 57 | &["Apache-2.0", "MIT", "SRCL-1.0"], |
| 58 | &vk, |
| 59 | ) |
| 60 | .expect("Valid signed training grant must be accepted"); |
| 61 | |
| 62 | assert_eq!(bundle.bundle_kind, BundleKind::Training); |
| 63 | assert_eq!(bundle.records.len(), 1); |
| 64 | assert_ne!(bundle.merkle_root, aien_protocol_types::Digest32::ZERO); |
| 65 | assert_eq!( |
| 66 | bundle.records[0].source_uri, |
| 67 | "https://hf.co/datasets/aien/sovereign-math" |
| 68 | ); |
| 69 | } |
| 70 | |
| 71 | #[test] |
| 72 | fn test_unsigned_grant_rejected_fails_closed() { |
| 73 | let (_sk, vk) = create_authority_keys(); |
| 74 | let grant = SourceGrant::new( |
| 75 | "https://hf.co/datasets/aien/unsigned-dataset".to_string(), |
| 76 | "Apache-2.0".to_string(), |
| 77 | GrantPermissions::TRAIN, |
| 78 | "Unauthenticated Issuer".to_string(), |
| 79 | ); |
| 80 | |
| 81 | let pairs = vec![create_test_pair()]; |
| 82 | let res = RightsGate::validate_and_bundle( |
| 83 | &grant, |
| 84 | &pairs, |
| 85 | BundleKind::Training, |
| 86 | &["Apache-2.0", "MIT", "SRCL-1.0"], |
| 87 | &vk, |
| 88 | ); |
| 89 | |
| 90 | match res { |
| 91 | Err(ProvenanceError::MissingSignature) => {} |
| 92 | other => panic!( |
| 93 | "Expected MissingSignature for unsigned grant, got {:?}", |
| 94 | other |
| 95 | ), |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | #[test] |
| 100 | fn test_unapproved_license_rejected() { |
| 101 | let (sk, vk) = create_authority_keys(); |
| 102 | let mut grant = SourceGrant::new( |
| 103 | "https://example.com/proprietary-dataset".to_string(), |
| 104 | "CC-BY-NC-4.0".to_string(), |
| 105 | GrantPermissions::TRAIN, |
| 106 | "Untrusted Third Party".to_string(), |
| 107 | ); |
| 108 | grant.sign(&sk); |
| 109 | |
| 110 | let pairs = vec![create_test_pair()]; |
| 111 | let res = RightsGate::validate_and_bundle( |
| 112 | &grant, |
| 113 | &pairs, |
| 114 | BundleKind::Training, |
| 115 | &["Apache-2.0", "MIT", "SRCL-1.0"], |
| 116 | &vk, |
| 117 | ); |
| 118 | |
| 119 | match res { |
| 120 | Err(ProvenanceError::IncompatibleLicense { license }) => { |
| 121 | assert_eq!(license, "CC-BY-NC-4.0"); |
| 122 | } |
| 123 | other => panic!("Expected IncompatibleLicense, got {:?}", other), |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | #[test] |
| 128 | fn test_missing_permission_rejected() { |
| 129 | let (sk, vk) = create_authority_keys(); |
| 130 | let mut grant = SourceGrant::new( |
| 131 | "https://hf.co/datasets/aien/eval-set".to_string(), |
| 132 | "MIT".to_string(), |
| 133 | GrantPermissions::EVALUATE, // Only EVALUATE, not TRAIN |
| 134 | "Atlas Rights Authority".to_string(), |
| 135 | ); |
| 136 | grant.sign(&sk); |
| 137 | |
| 138 | let pairs = vec![create_test_pair()]; |
| 139 | let res = RightsGate::validate_and_bundle( |
| 140 | &grant, |
| 141 | &pairs, |
| 142 | BundleKind::Training, |
| 143 | &["Apache-2.0", "MIT", "SRCL-1.0"], |
| 144 | &vk, |
| 145 | ); |
| 146 | |
| 147 | match res { |
| 148 | Err(ProvenanceError::PermissionDenied { required, granted }) => { |
| 149 | assert_eq!(required, GrantPermissions::TRAIN); |
| 150 | assert_eq!(granted, GrantPermissions::EVALUATE); |
| 151 | } |
| 152 | other => panic!("Expected PermissionDenied, got {:?}", other), |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | #[test] |
| 157 | fn test_expired_grant_rejected() { |
| 158 | let (sk, vk) = create_authority_keys(); |
| 159 | let mut grant = SourceGrant::new( |
| 160 | "https://hf.co/datasets/aien/expired-set".to_string(), |
| 161 | "MIT".to_string(), |
| 162 | GrantPermissions::TRAIN, |
| 163 | "Atlas Rights Authority".to_string(), |
| 164 | ); |
| 165 | grant.expires_at_epoch_sec = Some(1000); // Far in the past |
| 166 | grant.sign(&sk); |
| 167 | |
| 168 | let pairs = vec![create_test_pair()]; |
| 169 | let res = RightsGate::validate_and_bundle( |
| 170 | &grant, |
| 171 | &pairs, |
| 172 | BundleKind::Training, |
| 173 | &["Apache-2.0", "MIT", "SRCL-1.0"], |
| 174 | &vk, |
| 175 | ); |
| 176 | |
| 177 | match res { |
| 178 | Err(ProvenanceError::GrantExpired(expires_at)) => { |
| 179 | assert_eq!(expires_at, 1000); |
| 180 | } |
| 181 | other => panic!("Expected GrantExpired, got {:?}", other), |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | #[test] |
| 186 | fn test_cryptographic_signature_verification() { |
| 187 | let signing_key = SigningKey::from_slice(&[33u8; 32]).expect("valid key"); |
| 188 | let verifying_key = *signing_key.verifying_key(); |
| 189 | |
| 190 | let mut grant = SourceGrant::new( |
| 191 | "https://hf.co/datasets/aien/signed-set".to_string(), |
| 192 | "MIT".to_string(), |
| 193 | GrantPermissions::TRAIN, |
| 194 | "Atlas Rights Authority".to_string(), |
| 195 | ); |
| 196 | grant.sign(&signing_key); |
| 197 | |
| 198 | let pairs = vec![create_test_pair()]; |
| 199 | let bundle = RightsGate::validate_and_bundle( |
| 200 | &grant, |
| 201 | &pairs, |
| 202 | BundleKind::Training, |
| 203 | &["Apache-2.0", "MIT", "SRCL-1.0"], |
| 204 | &verifying_key, |
| 205 | ) |
| 206 | .expect("Signed grant must be accepted"); |
| 207 | assert_eq!(bundle.records.len(), 1); |
| 208 | |
| 209 | // Tampering with grant fails signature verification |
| 210 | let mut tampered_grant = grant.clone(); |
| 211 | tampered_grant.license_spdx = "Apache-2.0".to_string(); |
| 212 | let tamper_res = RightsGate::validate_and_bundle( |
| 213 | &tampered_grant, |
| 214 | &pairs, |
| 215 | BundleKind::Training, |
| 216 | &["Apache-2.0", "MIT", "SRCL-1.0"], |
| 217 | &verifying_key, |
| 218 | ); |
| 219 | match tamper_res { |
| 220 | Err(ProvenanceError::SignatureVerificationFailed(_)) => {} |
| 221 | other => panic!("Expected SignatureVerificationFailed, got {:?}", other), |
| 222 | } |
| 223 | |
| 224 | // Wrong authority key fails signature verification |
| 225 | let wrong_signing_key = SigningKey::from_slice(&[99u8; 32]).unwrap(); |
| 226 | let wrong_verifying_key = *wrong_signing_key.verifying_key(); |
| 227 | let wrong_key_res = RightsGate::validate_and_bundle( |
| 228 | &grant, |
| 229 | &pairs, |
| 230 | BundleKind::Training, |
| 231 | &["Apache-2.0", "MIT", "SRCL-1.0"], |
| 232 | &wrong_verifying_key, |
| 233 | ); |
| 234 | match wrong_key_res { |
| 235 | Err(ProvenanceError::SignatureVerificationFailed(_)) => {} |
| 236 | other => panic!( |
| 237 | "Expected SignatureVerificationFailed with wrong key, got {:?}", |
| 238 | other |
| 239 | ), |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | #[test] |
| 244 | fn test_evidence_bundle_quarantine_enforced() { |
| 245 | let (sk, vk) = create_authority_keys(); |
| 246 | let mut grant = SourceGrant::new( |
| 247 | "https://hf.co/datasets/aien/benchmark-evidence".to_string(), |
| 248 | "MIT".to_string(), |
| 249 | GrantPermissions::EVALUATE, |
| 250 | "Atlas Rights Authority".to_string(), |
| 251 | ); |
| 252 | grant.sign(&sk); |
| 253 | |
| 254 | let pairs = vec![create_test_pair()]; |
| 255 | let bundle = RightsGate::validate_and_bundle( |
| 256 | &grant, |
| 257 | &pairs, |
| 258 | BundleKind::Evidence, |
| 259 | &["Apache-2.0", "MIT", "SRCL-1.0"], |
| 260 | &vk, |
| 261 | ) |
| 262 | .expect("Evidence grant must be accepted"); |
| 263 | |
| 264 | // The quarantine gate scans every path component for train/sft/dpo/distill, |
| 265 | // so a random temp-dir name can trip it by accident. Draw until the name is clean. |
| 266 | let tmp = loop { |
| 267 | let t = tempfile::tempdir().unwrap(); |
| 268 | let name = t |
| 269 | .path() |
| 270 | .file_name() |
| 271 | .unwrap() |
| 272 | .to_string_lossy() |
| 273 | .to_lowercase(); |
| 274 | if !["train", "sft", "dpo", "distill"] |
| 275 | .iter() |
| 276 | .any(|b| name.contains(b)) |
| 277 | { |
| 278 | break t; |
| 279 | } |
| 280 | }; |
| 281 | |
| 282 | // 1. Attempting to export evidence bundle to training path fails closed |
| 283 | let train_path = tmp.path().join("train_dataset.jsonl"); |
| 284 | let err = RightsGate::export_bundle(&bundle, &train_path).unwrap_err(); |
| 285 | assert_eq!(err.kind(), ErrorKind::PermissionDenied); |
| 286 | |
| 287 | let sft_path = tmp.path().join("sft.jsonl"); |
| 288 | let err_sft = RightsGate::export_bundle(&bundle, &sft_path).unwrap_err(); |
| 289 | assert_eq!(err_sft.kind(), ErrorKind::PermissionDenied); |
| 290 | |
| 291 | let distill_path = tmp.path().join("distill_dataset.jsonl"); |
| 292 | let err_distill = RightsGate::export_bundle(&bundle, &distill_path).unwrap_err(); |
| 293 | assert_eq!(err_distill.kind(), ErrorKind::PermissionDenied); |
| 294 | |
| 295 | // 2. Exporting to path without quarantine/evidence directory or name fails closed |
| 296 | let general_path = tmp.path().join("output.jsonl"); |
| 297 | let err_general = RightsGate::export_bundle(&bundle, &general_path).unwrap_err(); |
| 298 | assert_eq!(err_general.kind(), ErrorKind::PermissionDenied); |
| 299 | |
| 300 | // 3. Exporting to quarantine evidence path succeeds |
| 301 | let evidence_path = tmp.path().join("evidence_audit.jsonl"); |
| 302 | let count = |
| 303 | RightsGate::export_bundle(&bundle, &evidence_path).expect("Evidence export must succeed"); |
| 304 | assert_eq!(count, 1); |
| 305 | assert!(evidence_path.exists()); |
| 306 | |
| 307 | let quarantine_dir = tmp.path().join("quarantine"); |
| 308 | std::fs::create_dir_all(&quarantine_dir).unwrap(); |
| 309 | let q_path = quarantine_dir.join("records.jsonl"); |
| 310 | let q_count = |
| 311 | RightsGate::export_bundle(&bundle, &q_path).expect("Quarantine export must succeed"); |
| 312 | assert_eq!(q_count, 1); |
| 313 | assert!(q_path.exists()); |
| 314 | } |