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-runtime / tests / refusal_table_test.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 | //! sovereign-core #267 follow-up: a security fix must not change the refusal |
| 17 | //! an old caller gets. This table drives the REAL `authorize` (mint_grant) and |
| 18 | //! `open_intent` over eight target states and records the refusal KIND each |
| 19 | //! gets. The same file ran on origin/main (3acc2cb) to produce EXPECTED; the |
| 20 | //! branch must give the identical kind for every row. |
| 21 | //! |
| 22 | //! Needs the linked composition archive; IGNORED in a stub build. |
| 23 | #[path = "support/home_guard.rs"] |
| 24 | mod home_guard; |
| 25 | |
| 26 | use aien_omega_compose::hex; |
| 27 | use aien_runtime::compose::{ComposeBridge, ComposeProposer, Generation}; |
| 28 | use aien_runtime::control::{ComposeNoteReport, ComposeTaskReport, ControlResponse}; |
| 29 | use aien_runtime::effects::{self, IntentRequest, MintRequest}; |
| 30 | use sha2::{Digest, Sha256}; |
| 31 | use std::os::unix::fs::PermissionsExt; |
| 32 | use std::path::PathBuf; |
| 33 | use std::sync::Arc; |
| 34 | |
| 35 | const PROPOSAL: &str = "filename: d/NOTES.md\nkeep every change inside the workspace\n"; |
| 36 | const CONTENT: &str = "keep every change inside the workspace\n"; |
| 37 | |
| 38 | fn proposer() -> ComposeProposer { |
| 39 | Arc::new(|_p: &str, _l: std::time::Duration| { |
| 40 | Ok(Generation { |
| 41 | text: PROPOSAL.to_string(), |
| 42 | tokens: 8, |
| 43 | finish_reason: Some("eos".into()), |
| 44 | ..Default::default() |
| 45 | }) |
| 46 | }) |
| 47 | } |
| 48 | |
| 49 | fn sha(b: &[u8]) -> String { |
| 50 | hex(&Sha256::digest(b)) |
| 51 | } |
| 52 | |
| 53 | static ONE_HOME: std::sync::Mutex<()> = std::sync::Mutex::new(()); |
| 54 | |
| 55 | struct Fx { |
| 56 | _turn: std::sync::MutexGuard<'static, ()>, |
| 57 | _tmp: tempfile::TempDir, |
| 58 | root: PathBuf, |
| 59 | ws: PathBuf, |
| 60 | b: ComposeBridge, |
| 61 | report: ComposeTaskReport, |
| 62 | } |
| 63 | |
| 64 | impl Fx { |
| 65 | fn new() -> Option<Fx> { |
| 66 | let turn = ONE_HOME.lock().unwrap_or_else(|e| e.into_inner()); |
| 67 | let tmp = tempfile::tempdir().unwrap(); |
| 68 | let root = std::fs::canonicalize(tmp.path()).unwrap(); |
| 69 | let ws = root.join("ws"); |
| 70 | std::fs::create_dir_all(ws.join("d")).unwrap(); |
| 71 | let b = ComposeBridge::new(root.join("compose"), proposer(), "test:fixed-proposer") |
| 72 | .with_authorize_requires_desk(false); |
| 73 | let run = b.run_task("write the note", ws.to_str().unwrap()); |
| 74 | if !aien_omega_compose::LINKED { |
| 75 | return None; |
| 76 | } |
| 77 | let ControlResponse::ComposeTaskResult(report) = run else { |
| 78 | panic!("run: {run:?}") |
| 79 | }; |
| 80 | assert!(report.committed, "{report:?}"); |
| 81 | Some(Fx { |
| 82 | _turn: turn, |
| 83 | _tmp: tmp, |
| 84 | root, |
| 85 | ws, |
| 86 | b, |
| 87 | report: *report, |
| 88 | }) |
| 89 | } |
| 90 | fn file(&self) -> PathBuf { |
| 91 | self.ws.join("d/NOTES.md") |
| 92 | } |
| 93 | fn mint_req(&self) -> MintRequest { |
| 94 | MintRequest { |
| 95 | cx_promotion: self.report.cx_promotion, |
| 96 | proposal_sha256: sha(PROPOSAL.as_bytes()), |
| 97 | workspace: self.ws.display().to_string(), |
| 98 | approver: "drake".into(), |
| 99 | constraints: vec![], |
| 100 | } |
| 101 | } |
| 102 | fn req(&self, a: u64, wrong_grant: bool) -> IntentRequest { |
| 103 | let (pid, start) = effects::self_executor(); |
| 104 | IntentRequest { |
| 105 | authorization: a, |
| 106 | proposal_sha256: sha(PROPOSAL.as_bytes()), |
| 107 | path: "d/NOTES.md".into(), |
| 108 | target: self.file().display().to_string(), |
| 109 | content_sha256: if wrong_grant { |
| 110 | sha(b"other") |
| 111 | } else { |
| 112 | sha(CONTENT.as_bytes()) |
| 113 | }, |
| 114 | executor_pid: pid, |
| 115 | executor_start: start, |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | /// The refusal kind of a response: "OK", the EFFECT_REFUSED name, or the |
| 121 | /// first word of a plain error. |
| 122 | fn kind(r: &ControlResponse) -> String { |
| 123 | match r { |
| 124 | ControlResponse::Error(e) => match e.strip_prefix("EFFECT_REFUSED ") { |
| 125 | Some(rest) => rest.split(':').next().unwrap().to_string(), |
| 126 | None => format!("plain:{}", e.split_whitespace().next().unwrap_or("")), |
| 127 | }, |
| 128 | ControlResponse::ComposeNoted(_) => "OK".into(), |
| 129 | other => format!("other:{other:?}"), |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | fn id(r: ControlResponse) -> u64 { |
| 134 | match r { |
| 135 | ControlResponse::ComposeNoted(ComposeNoteReport { id, .. }) => id, |
| 136 | other => panic!("expected a note, got {other:?}"), |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | /// How the world is shaped before the call. |
| 141 | #[derive(Clone, Copy, Debug)] |
| 142 | enum World { |
| 143 | Valid, |
| 144 | Missing, |
| 145 | Directory, |
| 146 | PermissionDenied, |
| 147 | LeafSymlinkInside, |
| 148 | LeafSymlinkOutside, |
| 149 | IntermediateSymlinkSameBytes, |
| 150 | IntermediateSymlinkOtherBytes, |
| 151 | MissingParent, |
| 152 | } |
| 153 | |
| 154 | const WORLDS: [World; 9] = [ |
| 155 | World::Valid, |
| 156 | World::Missing, |
| 157 | World::Directory, |
| 158 | World::PermissionDenied, |
| 159 | World::LeafSymlinkInside, |
| 160 | World::LeafSymlinkOutside, |
| 161 | World::IntermediateSymlinkSameBytes, |
| 162 | World::IntermediateSymlinkOtherBytes, |
| 163 | World::MissingParent, |
| 164 | ]; |
| 165 | |
| 166 | fn shape(fx: &Fx, w: World) { |
| 167 | let f = fx.file(); |
| 168 | let d = fx.ws.join("d"); |
| 169 | let outside = fx.root.join("outside"); |
| 170 | match w { |
| 171 | World::Valid => {} |
| 172 | World::Missing => std::fs::remove_file(&f).unwrap(), |
| 173 | World::Directory => { |
| 174 | std::fs::remove_file(&f).unwrap(); |
| 175 | std::fs::create_dir(&f).unwrap(); |
| 176 | } |
| 177 | World::PermissionDenied => { |
| 178 | std::fs::set_permissions(&f, std::fs::Permissions::from_mode(0o000)).unwrap() |
| 179 | } |
| 180 | World::LeafSymlinkInside => { |
| 181 | std::fs::write(d.join("other"), "old\n").unwrap(); |
| 182 | std::fs::remove_file(&f).unwrap(); |
| 183 | std::os::unix::fs::symlink(d.join("other"), &f).unwrap(); |
| 184 | } |
| 185 | World::LeafSymlinkOutside => { |
| 186 | std::fs::create_dir_all(&outside).unwrap(); |
| 187 | std::fs::write(outside.join("x"), "old\n").unwrap(); |
| 188 | std::fs::remove_file(&f).unwrap(); |
| 189 | std::os::unix::fs::symlink(outside.join("x"), &f).unwrap(); |
| 190 | } |
| 191 | World::IntermediateSymlinkSameBytes | World::IntermediateSymlinkOtherBytes => { |
| 192 | std::fs::create_dir_all(&outside).unwrap(); |
| 193 | let bytes = if matches!(w, World::IntermediateSymlinkSameBytes) { |
| 194 | "old\n" |
| 195 | } else { |
| 196 | "different\n" |
| 197 | }; |
| 198 | std::fs::write(outside.join("NOTES.md"), bytes).unwrap(); |
| 199 | std::fs::rename(&d, fx.ws.join("d.held")).unwrap(); |
| 200 | std::os::unix::fs::symlink(&outside, &d).unwrap(); |
| 201 | } |
| 202 | World::MissingParent => std::fs::remove_dir_all(&d).unwrap(), |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | /// Rows: "<world> | <op>" -> kind. `wrong_grant` rows use a request whose |
| 207 | /// content differs from the grant (NotAuthorized on a readable target). |
| 208 | fn run_table() -> Vec<(String, String)> { |
| 209 | let mut rows = Vec::new(); |
| 210 | for w in WORLDS { |
| 211 | // authorize: the world is shaped, then the mint is asked. |
| 212 | { |
| 213 | let fx = Fx::new().unwrap(); |
| 214 | std::fs::write(fx.file(), "old\n").unwrap(); |
| 215 | shape(&fx, w); |
| 216 | let r = effects::mint_grant(&fx.b, &fx.mint_req()); |
| 217 | rows.push((format!("{w:?} | authorize"), kind(&r))); |
| 218 | } |
| 219 | // open_intent: a valid grant is minted on "old\n", then the world is shaped. |
| 220 | for wrong in [false, true] { |
| 221 | let fx = Fx::new().unwrap(); |
| 222 | std::fs::write(fx.file(), "old\n").unwrap(); |
| 223 | let g = id(effects::mint_grant(&fx.b, &fx.mint_req())); |
| 224 | shape(&fx, w); |
| 225 | let r = effects::open_intent(&fx.b, &fx.req(g, wrong)); |
| 226 | let op = if wrong { |
| 227 | "open_intent(wrong grant)" |
| 228 | } else { |
| 229 | "open_intent" |
| 230 | }; |
| 231 | rows.push((format!("{w:?} | {op}"), kind(&r))); |
| 232 | } |
| 233 | // restore permissions so the temp dir can be removed |
| 234 | let _ = w; |
| 235 | } |
| 236 | rows |
| 237 | } |
| 238 | |
| 239 | /// origin/main (3acc2cb) results for the same table (this file run there). |
| 240 | const EXPECTED: &[(&str, &str)] = &[ |
| 241 | ("Valid | authorize", "OK"), |
| 242 | ("Valid | open_intent", "OK"), |
| 243 | ("Valid | open_intent(wrong grant)", "NotAuthorized"), |
| 244 | ("Missing | authorize", "OK"), |
| 245 | ("Missing | open_intent", "Stale"), |
| 246 | ("Missing | open_intent(wrong grant)", "NotAuthorized"), |
| 247 | ("Directory | authorize", "OutsideWorkspace"), |
| 248 | ("Directory | open_intent", "Stale"), |
| 249 | ("Directory | open_intent(wrong grant)", "Stale"), |
| 250 | ("PermissionDenied | authorize", "OutsideWorkspace"), |
| 251 | ("PermissionDenied | open_intent", "Stale"), |
| 252 | ("PermissionDenied | open_intent(wrong grant)", "Stale"), |
| 253 | ("LeafSymlinkInside | authorize", "OutsideWorkspace"), |
| 254 | ("LeafSymlinkInside | open_intent", "Stale"), |
| 255 | ("LeafSymlinkInside | open_intent(wrong grant)", "Stale"), |
| 256 | ("LeafSymlinkOutside | authorize", "OutsideWorkspace"), |
| 257 | ("LeafSymlinkOutside | open_intent", "Stale"), |
| 258 | ("LeafSymlinkOutside | open_intent(wrong grant)", "Stale"), |
| 259 | ( |
| 260 | "IntermediateSymlinkSameBytes | authorize", |
| 261 | "OutsideWorkspace", |
| 262 | ), |
| 263 | ( |
| 264 | "IntermediateSymlinkSameBytes | open_intent", |
| 265 | "OutsideWorkspace", |
| 266 | ), |
| 267 | ( |
| 268 | "IntermediateSymlinkSameBytes | open_intent(wrong grant)", |
| 269 | "NotAuthorized", |
| 270 | ), |
| 271 | ( |
| 272 | "IntermediateSymlinkOtherBytes | authorize", |
| 273 | "OutsideWorkspace", |
| 274 | ), |
| 275 | ("IntermediateSymlinkOtherBytes | open_intent", "Stale"), |
| 276 | ( |
| 277 | "IntermediateSymlinkOtherBytes | open_intent(wrong grant)", |
| 278 | "NotAuthorized", |
| 279 | ), |
| 280 | ("MissingParent | authorize", "OutsideWorkspace"), |
| 281 | ("MissingParent | open_intent", "Stale"), |
| 282 | ("MissingParent | open_intent(wrong grant)", "NotAuthorized"), |
| 283 | ]; |
| 284 | |
| 285 | /// The one row where the branch deliberately differs from origin/main. It is |
| 286 | /// still a refusal and stricter, not a weakening: main read the bytes THROUGH |
| 287 | /// the symlinked directory and said Stale; the branch never reads through the |
| 288 | /// symlink and says OutsideWorkspace. Telling Stale would need the outside |
| 289 | /// bytes: the very bug. |
| 290 | const DIFFERS: &[(&str, &str)] = &[( |
| 291 | "IntermediateSymlinkOtherBytes | open_intent", |
| 292 | "OutsideWorkspace", |
| 293 | )]; |
| 294 | |
| 295 | #[test] |
| 296 | #[cfg_attr(not(compose_linked), ignore = "needs the linked librx_compose.a")] |
| 297 | fn refusal_kinds_match_origin_main() { |
| 298 | let _home = home_guard::home_slot(); |
| 299 | if !aien_omega_compose::LINKED { |
| 300 | return; |
| 301 | } |
| 302 | let rows = run_table(); |
| 303 | for (k, v) in &rows { |
| 304 | println!("ROW {k} => {v}"); |
| 305 | } |
| 306 | assert_eq!(rows.len(), EXPECTED.len()); |
| 307 | for (k, v) in &rows { |
| 308 | let main = EXPECTED |
| 309 | .iter() |
| 310 | .find(|(ek, _)| ek == k) |
| 311 | .unwrap_or_else(|| panic!("no expectation for {k}")) |
| 312 | .1; |
| 313 | let want = DIFFERS.iter().find(|(dk, _)| dk == k).map_or(main, |d| d.1); |
| 314 | assert_eq!(v, want, "{k}: branch gives {v}, origin/main gave {main}"); |
| 315 | } |
| 316 | } |