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 / minted_grant_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 #261: the daemon honours only grants it minted itself. |
| 17 | //! |
| 18 | //! The ordinary operator flow (propose -> authorize -> execute) is driven at |
| 19 | //! the bridge, exactly as the daemon runs it (no socket: the mock backend of |
| 20 | //! the socket tests cannot commit a proposal). Every test needs the linked |
| 21 | //! composition archive and is IGNORED in a stub build, never passed. The |
| 22 | //! socket-level refusal of a raw `authorization` note is |
| 23 | //! `c18_a1_raw_forged_authorization` in approved_attacks_test.rs. |
| 24 | #[path = "support/home_guard.rs"] |
| 25 | mod home_guard; |
| 26 | |
| 27 | use aien_omega_compose::hex; |
| 28 | use aien_runtime::compose::{ComposeBridge, ComposeProposer, Generation}; |
| 29 | use aien_runtime::control::{ComposeNoteReport, ComposeTaskReport, ControlResponse}; |
| 30 | use aien_runtime::effects::{self, IntentRequest, MintRequest}; |
| 31 | use serde_json::{json, Value}; |
| 32 | use sha2::{Digest, Sha256}; |
| 33 | use std::path::{Path, PathBuf}; |
| 34 | use std::sync::Arc; |
| 35 | |
| 36 | const PROPOSAL: &str = "filename: NOTES.md\nkeep every change inside the workspace\n"; |
| 37 | const CONTENT: &str = "keep every change inside the workspace\n"; |
| 38 | |
| 39 | fn proposer() -> ComposeProposer { |
| 40 | Arc::new(|_prompt: &str, _limit: std::time::Duration| { |
| 41 | Ok(Generation { |
| 42 | text: PROPOSAL.to_string(), |
| 43 | tokens: 8, |
| 44 | finish_reason: Some("eos".into()), |
| 45 | ..Default::default() |
| 46 | }) |
| 47 | }) |
| 48 | } |
| 49 | |
| 50 | fn sha(b: &[u8]) -> String { |
| 51 | hex(&Sha256::digest(b)) |
| 52 | } |
| 53 | |
| 54 | fn noted(r: ControlResponse) -> ComposeNoteReport { |
| 55 | match r { |
| 56 | ControlResponse::ComposeNoted(n) => n, |
| 57 | other => panic!("expected ComposeNoted, got {other:?}"), |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | /// A stop, resume or revoke answer. |
| 62 | fn controlled(r: ControlResponse) -> aien_runtime::control::ComposeControlReport { |
| 63 | match r { |
| 64 | ControlResponse::ComposeControlled(c) => *c, |
| 65 | other => panic!("expected ComposeControlled, got {other:?}"), |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | /// The refusal text of `r`; panics when the attack succeeded. |
| 70 | fn refusal(r: ControlResponse, attack: &str) -> String { |
| 71 | match r { |
| 72 | ControlResponse::Error(e) => e, |
| 73 | other => panic!("ATTACK SUCCEEDED ({attack}): {other:?}"), |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | /// The composition engine holds one home at a time per process. |
| 78 | static ONE_HOME: std::sync::Mutex<()> = std::sync::Mutex::new(()); |
| 79 | |
| 80 | struct Fx { |
| 81 | _turn: std::sync::MutexGuard<'static, ()>, |
| 82 | _tmp: tempfile::TempDir, |
| 83 | home: PathBuf, |
| 84 | ws: PathBuf, |
| 85 | b: ComposeBridge, |
| 86 | report: ComposeTaskReport, |
| 87 | } |
| 88 | |
| 89 | impl Fx { |
| 90 | /// A home with one committed ordinary proposal; None in a stub build. |
| 91 | fn new() -> Option<Fx> { |
| 92 | let turn = ONE_HOME.lock().unwrap_or_else(|e| e.into_inner()); |
| 93 | let tmp = tempfile::tempdir().unwrap(); |
| 94 | let home = tmp.path().join("compose"); |
| 95 | let ws = std::fs::canonicalize({ |
| 96 | let w = tmp.path().join("ws"); |
| 97 | std::fs::create_dir_all(&w).unwrap(); |
| 98 | w |
| 99 | }) |
| 100 | .unwrap(); |
| 101 | let b = ComposeBridge::new(home.clone(), proposer(), "test:fixed-proposer") |
| 102 | .with_authorize_requires_desk(false); |
| 103 | let run = b.run_task("write the note", ws.to_str().unwrap()); |
| 104 | if !aien_omega_compose::LINKED { |
| 105 | assert!(matches!(run, ControlResponse::Error(_))); |
| 106 | return None; |
| 107 | } |
| 108 | let ControlResponse::ComposeTaskResult(report) = run else { |
| 109 | panic!("run: {run:?}") |
| 110 | }; |
| 111 | assert!(report.committed, "{report:?}"); |
| 112 | Some(Fx { |
| 113 | _turn: turn, |
| 114 | _tmp: tmp, |
| 115 | home, |
| 116 | ws, |
| 117 | b, |
| 118 | report: *report, |
| 119 | }) |
| 120 | } |
| 121 | |
| 122 | fn target(&self) -> String { |
| 123 | self.ws.join("NOTES.md").display().to_string() |
| 124 | } |
| 125 | |
| 126 | fn mint_req(&self) -> MintRequest { |
| 127 | MintRequest { |
| 128 | cx_promotion: self.report.cx_promotion, |
| 129 | proposal_sha256: sha(PROPOSAL.as_bytes()), |
| 130 | workspace: self.ws.display().to_string(), |
| 131 | approver: "drake".into(), |
| 132 | constraints: vec![], |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | fn mint(&self) -> ControlResponse { |
| 137 | effects::mint_grant(&self.b, &self.mint_req()) |
| 138 | } |
| 139 | |
| 140 | fn req(&self, a: u64, alive: bool) -> IntentRequest { |
| 141 | let (pid, start) = if alive { |
| 142 | effects::self_executor() |
| 143 | } else { |
| 144 | (0, 0) |
| 145 | }; |
| 146 | IntentRequest { |
| 147 | authorization: a, |
| 148 | proposal_sha256: sha(PROPOSAL.as_bytes()), |
| 149 | path: "NOTES.md".into(), |
| 150 | target: self.target(), |
| 151 | content_sha256: sha(CONTENT.as_bytes()), |
| 152 | executor_pid: pid, |
| 153 | executor_start: start, |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | fn open(&self, a: u64) -> ControlResponse { |
| 158 | effects::open_intent(&self.b, &self.req(a, true)) |
| 159 | } |
| 160 | |
| 161 | /// The text of record `id`. |
| 162 | fn text(&self, id: u64) -> Value { |
| 163 | match self.b.recall(&[id], None) { |
| 164 | ControlResponse::ComposeRecalled(r) => { |
| 165 | serde_json::from_str(r.cited[0].text.as_deref().unwrap()).unwrap() |
| 166 | } |
| 167 | other => panic!("recall: {other:?}"), |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | fn ack_state(&self, intent: u64) -> String { |
| 172 | let n = noted(effects::ack(&self.b, intent, &json!({"executor": "test"}))); |
| 173 | self.text(n.id)["state"].as_str().unwrap().to_string() |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | macro_rules! linked_test { |
| 178 | ($name:ident, |$fx:ident| $body:block) => { |
| 179 | #[test] |
| 180 | #[cfg_attr(not(compose_linked), ignore = "needs the linked librx_compose.a")] |
| 181 | fn $name() { |
| 182 | let _home = home_guard::home_slot(); |
| 183 | let Some($fx) = Fx::new() else { return }; |
| 184 | $body |
| 185 | } |
| 186 | }; |
| 187 | } |
| 188 | |
| 189 | linked_test!(ordinary_flow_end_to_end_with_a_daemon_minted_grant, |fx| { |
| 190 | // The run left the daemon's own commit record, and the report names it. |
| 191 | let commit = fx.report.compose_commit.expect("commit record id"); |
| 192 | let c = fx.text(commit); |
| 193 | assert_eq!(c["compose_commit"], 1); |
| 194 | assert_eq!(c["proposal_sha256"], sha(PROPOSAL.as_bytes())); |
| 195 | assert_eq!(c["workspace"], fx.ws.display().to_string()); |
| 196 | // Authorize, intent, write, ack: DONE. |
| 197 | let g = noted(fx.mint()); |
| 198 | let gt = fx.text(g.id); |
| 199 | assert_eq!(gt["minted_grant"], 1); |
| 200 | assert_eq!(gt["compose_commit"], commit); |
| 201 | assert_eq!(gt["target"], fx.target()); |
| 202 | assert!(g.links.contains(&commit), "{:?}", g.links); |
| 203 | let i = noted(fx.open(g.id)).id; |
| 204 | std::fs::write(fx.target(), CONTENT).unwrap(); |
| 205 | assert_eq!(fx.ack_state(i), "DONE"); |
| 206 | assert_eq!(std::fs::read_to_string(fx.target()).unwrap(), CONTENT); |
| 207 | }); |
| 208 | |
| 209 | linked_test!(a_replayed_grant_is_refused, |fx| { |
| 210 | let g = noted(fx.mint()).id; |
| 211 | let i = noted(fx.open(g)).id; |
| 212 | // Second intent while the first is open, then after it settled. |
| 213 | assert!(refusal(fx.open(g), "intent while open").contains("ReconciliationRequired")); |
| 214 | std::fs::write(fx.target(), CONTENT).unwrap(); |
| 215 | assert_eq!(fx.ack_state(i), "DONE"); |
| 216 | assert!(refusal(fx.open(g), "replay after DONE").contains("AlreadySpent")); |
| 217 | }); |
| 218 | |
| 219 | linked_test!(a_grant_names_only_what_the_daemon_committed, |fx| { |
| 220 | let g = noted(fx.mint()).id; |
| 221 | for (what, f) in [ |
| 222 | ( |
| 223 | "other path", |
| 224 | (|r: &mut IntentRequest| { |
| 225 | r.path = "OTHER.md".into(); |
| 226 | }) as fn(&mut IntentRequest), |
| 227 | ), |
| 228 | ("other content", |r| r.content_sha256 = sha(b"evil\n")), |
| 229 | ("other proposal", |r| r.proposal_sha256 = sha(b"evil")), |
| 230 | ("other target", |r| { |
| 231 | r.target = "/etc/passwd".into(); |
| 232 | }), |
| 233 | ] { |
| 234 | let mut r = fx.req(g, true); |
| 235 | f(&mut r); |
| 236 | let e = refusal(effects::open_intent(&fx.b, &r), what); |
| 237 | assert!(e.contains("NotAuthorized"), "{what}: {e}"); |
| 238 | } |
| 239 | assert!(!Path::new(&fx.target()).exists()); |
| 240 | }); |
| 241 | |
| 242 | linked_test!(minting_needs_a_proposal_the_daemon_committed, |fx| { |
| 243 | let mut r = fx.mint_req(); |
| 244 | r.proposal_sha256 = sha(b"never proposed by any compose"); |
| 245 | let e = refusal(effects::mint_grant(&fx.b, &r), "unknown proposal"); |
| 246 | assert!(e.contains("only a proposal this daemon committed"), "{e}"); |
| 247 | let mut r = fx.mint_req(); |
| 248 | r.cx_promotion += 1000; |
| 249 | refusal(effects::mint_grant(&fx.b, &r), "unknown promotion"); |
| 250 | // The workspace the compose ran for is the only one it can be authorized for. |
| 251 | let other = fx.ws.parent().unwrap().join("other"); |
| 252 | std::fs::create_dir_all(&other).unwrap(); |
| 253 | let mut r = fx.mint_req(); |
| 254 | r.workspace = other.display().to_string(); |
| 255 | let e = refusal(effects::mint_grant(&fx.b, &r), "other workspace"); |
| 256 | assert!(e.contains("OutsideWorkspace"), "{e}"); |
| 257 | let mut r = fx.mint_req(); |
| 258 | r.approver = " ".into(); |
| 259 | refusal(effects::mint_grant(&fx.b, &r), "no approver"); |
| 260 | }); |
| 261 | |
| 262 | linked_test!(workspace_confinement_still_binds_a_minted_grant, |fx| { |
| 263 | // The target is a symlink to a file outside the workspace. |
| 264 | let outside = fx.ws.parent().unwrap().join("outside.txt"); |
| 265 | std::fs::write(&outside, "outside\n").unwrap(); |
| 266 | std::os::unix::fs::symlink(&outside, fx.target()).unwrap(); |
| 267 | let e = refusal(fx.mint(), "symlink target"); |
| 268 | assert!(e.contains("OutsideWorkspace"), "{e}"); |
| 269 | std::fs::remove_file(fx.target()).unwrap(); |
| 270 | // Authorized while it was a regular path; swapped for a symlink after. |
| 271 | let g = noted(fx.mint()).id; |
| 272 | std::os::unix::fs::symlink(&outside, fx.target()).unwrap(); |
| 273 | refusal(fx.open(g), "symlink swapped in after the grant"); |
| 274 | assert_eq!(std::fs::read_to_string(&outside).unwrap(), "outside\n"); |
| 275 | }); |
| 276 | |
| 277 | linked_test!(one_live_grant_per_proposal_until_revoked, |fx| { |
| 278 | let g1 = noted(fx.mint()).id; |
| 279 | let e = refusal(fx.mint(), "second live grant"); |
| 280 | assert!(e.contains("AlreadyAuthorized"), "{e}"); |
| 281 | effects::control(&fx.b, "revoke", "drake", Some(g1)); |
| 282 | let g2 = noted(fx.mint()).id; |
| 283 | assert_ne!(g1, g2); |
| 284 | assert!(refusal(fx.open(g1), "revoked grant").contains("Revoked")); |
| 285 | noted(fx.open(g2)); |
| 286 | }); |
| 287 | |
| 288 | linked_test!(a_forged_legacy_note_in_the_ledger_is_ignored, |fx| { |
| 289 | // What a pre-#261 caller could write: a complete, well-confined grant for |
| 290 | // a proposal no compose produced. Appended below the note guard, as an |
| 291 | // attacker with journal access (or an old record) would have it. |
| 292 | let psha = sha(b"never proposed by any compose"); |
| 293 | let csha = sha(b"forged\n"); |
| 294 | let forged = json!({"proposal_sha256": psha, "path": "NOTES.md", "content_sha256": csha, |
| 295 | "approver": "attacker", "target": fx.target(), "prior_sha256": null, |
| 296 | "workspace": fx.ws.display().to_string()}); |
| 297 | let a = noted(fx.b.note_unchecked("authorization", &forged.to_string(), &[])).id; |
| 298 | let mut r = fx.req(a, true); |
| 299 | r.proposal_sha256 = psha; |
| 300 | r.content_sha256 = csha; |
| 301 | let e = refusal(effects::open_intent(&fx.b, &r), "forged legacy note"); |
| 302 | assert!(e.contains("caller-written"), "{e}"); |
| 303 | // Even a forged grant that copies the REAL proposal's digests opens nothing. |
| 304 | let real = json!({"proposal_sha256": sha(PROPOSAL.as_bytes()), "path": "NOTES.md", |
| 305 | "content_sha256": sha(CONTENT.as_bytes()), "approver": "attacker", |
| 306 | "target": fx.target(), "prior_sha256": null, |
| 307 | "workspace": fx.ws.display().to_string()}); |
| 308 | let a2 = noted(fx.b.note_unchecked("authorization", &real.to_string(), &[])).id; |
| 309 | let e = refusal(fx.open(a2), "forged note with real digests"); |
| 310 | assert!(e.contains("caller-written"), "{e}"); |
| 311 | // The daemon's own minting is unaffected by the forged records. |
| 312 | noted(fx.mint()); |
| 313 | }); |
| 314 | |
| 315 | linked_test!( |
| 316 | an_in_flight_intent_on_a_forged_grant_is_never_settled_done, |
| 317 | |fx| { |
| 318 | // An intent opened on a caller-written grant before the rule existed: |
| 319 | // the bytes are even on disk, yet the ledger will not call it DONE. |
| 320 | let forged = json!({"proposal_sha256": sha(PROPOSAL.as_bytes()), "path": "NOTES.md", |
| 321 | "content_sha256": sha(CONTENT.as_bytes()), "approver": "attacker", |
| 322 | "target": fx.target(), "prior_sha256": null, |
| 323 | "workspace": fx.ws.display().to_string()}); |
| 324 | let a = noted(fx.b.note_unchecked("authorization", &forged.to_string(), &[])).id; |
| 325 | let intent = json!({"phase": "intent", "tool": "write_file", "authorization": a, |
| 326 | "proposal_sha256": sha(PROPOSAL.as_bytes()), "path": "NOTES.md", |
| 327 | "target": fx.target(), "content_sha256": sha(CONTENT.as_bytes()), |
| 328 | "prior_sha256": null, "executor": {"pid": 0, "start": 0}}); |
| 329 | let i = noted(fx.b.note_unchecked("effect", &intent.to_string(), &[a])).id; |
| 330 | std::fs::write(fx.target(), CONTENT).unwrap(); |
| 331 | assert_eq!(fx.ack_state(i), "UNRESOLVED"); |
| 332 | let d = aien_runtime::control::ReconcileDeclare { |
| 333 | intent: i, |
| 334 | state: "done".into(), |
| 335 | approver: "drake".into(), |
| 336 | }; |
| 337 | let e = refusal(effects::reconcile(&fx.b, Some(&d), "test"), "declare DONE"); |
| 338 | assert!(e.contains("NotDaemonMinted"), "{e}"); |
| 339 | } |
| 340 | ); |
| 341 | |
| 342 | linked_test!( |
| 343 | a_minted_grant_survives_a_restart_and_recovery_still_settles, |
| 344 | |fx| { |
| 345 | let g = noted(fx.mint()).id; |
| 346 | let i = noted(effects::open_intent(&fx.b, &fx.req(g, false))).id; |
| 347 | let Fx { home, ws, b, .. } = fx; |
| 348 | drop(b); |
| 349 | // New daemon process: the ledger is rebuilt from the journal alone. |
| 350 | let b = ComposeBridge::new(home, proposer(), "test:fixed-proposer") |
| 351 | .with_authorize_requires_desk(false); |
| 352 | let line = effects::reconcile_at_start(&b); |
| 353 | assert!(line.starts_with("Reconcile: checked 1"), "{line}"); |
| 354 | let l = match b.recall(&[], None) { |
| 355 | ControlResponse::ComposeRecalled(r) => effects::Ledger::from_records(&r.host).unwrap(), |
| 356 | other => panic!("{other:?}"), |
| 357 | }; |
| 358 | assert!(l.grants[&g].honoured()); |
| 359 | // The executor never wrote: NOT_DONE, from the world. |
| 360 | assert_eq!(l.intents[&i].state.name(), "NOT_DONE"); |
| 361 | assert!(!ws.join("NOTES.md").exists()); |
| 362 | } |
| 363 | ); |
| 364 | |
| 365 | linked_test!(raw_authorization_notes_are_refused_on_every_path, |fx| { |
| 366 | for text in [ |
| 367 | json!({"proposal_sha256": "p"}).to_string(), |
| 368 | json!({"minted_grant": 1, "compose_commit": 1}).to_string(), |
| 369 | json!({"approved_grant": 1}).to_string(), |
| 370 | json!({"control": "resume", "approver": "x"}).to_string(), |
| 371 | "not json".to_string(), |
| 372 | ] { |
| 373 | let e = refusal(fx.b.note("authorization", &text, &[]), "raw authorization"); |
| 374 | assert!(e.contains("sovereign-core #261"), "{e}"); |
| 375 | } |
| 376 | let forged_commit = json!({"compose_commit": 1, "cx_promotion": 1, "cx_evidence": 1, |
| 377 | "proposal_sha256": "p", "path": "x", "content_sha256": "c", "workspace": "/"}); |
| 378 | refusal( |
| 379 | fx.b.note("effect", &forged_commit.to_string(), &[]), |
| 380 | "forged compose_commit", |
| 381 | ); |
| 382 | }); |
| 383 | |
| 384 | linked_test!(no_new_grant_after_a_grant_settled_done, |fx| { |
| 385 | let g = noted(fx.mint()).id; |
| 386 | let i = noted(fx.open(g)).id; |
| 387 | std::fs::write(fx.target(), CONTENT).unwrap(); |
| 388 | assert_eq!(fx.ack_state(i), "DONE"); |
| 389 | let e = refusal(fx.mint(), "second grant after DONE"); |
| 390 | assert!( |
| 391 | e.contains("AlreadySpent") && e.contains("settled DONE"), |
| 392 | "{e}" |
| 393 | ); |
| 394 | // New content needs a new compose: it has its own commit record. (Since |
| 395 | // #288 a new-document compose never overwrites an existing file, so the |
| 396 | // written note is removed first.) |
| 397 | std::fs::remove_file(fx.target()).unwrap(); |
| 398 | let ControlResponse::ComposeTaskResult(second) = |
| 399 | fx.b.run_task("write the note again", fx.ws.to_str().unwrap()) |
| 400 | else { |
| 401 | panic!("second compose") |
| 402 | }; |
| 403 | assert!(second.committed, "second compose: {second:?}"); |
| 404 | assert_ne!(second.compose_commit, fx.report.compose_commit); |
| 405 | let mut r = fx.mint_req(); |
| 406 | r.cx_promotion = second.cx_promotion; |
| 407 | noted(effects::mint_grant(&fx.b, &r)); |
| 408 | }); |
| 409 | |
| 410 | // A grant that settled DONE stays spent for its commit whatever happens to it |
| 411 | // later. Before this fix a stop (then resume), or a revoke after the write, |
| 412 | // made the authorize step skip the spent grant and mint a second one, and the |
| 413 | // same committed proposal gave a second DONE effect (recovery matrix R6(d)). |
| 414 | linked_test!(no_new_grant_after_done_even_after_stop_and_resume, |fx| { |
| 415 | let g = noted(fx.mint()).id; |
| 416 | let i = noted(fx.open(g)).id; |
| 417 | std::fs::write(fx.target(), CONTENT).unwrap(); |
| 418 | assert_eq!(fx.ack_state(i), "DONE"); |
| 419 | controlled(effects::control(&fx.b, "stop", "drake", None)); |
| 420 | controlled(effects::control(&fx.b, "resume", "drake", None)); |
| 421 | let e = refusal(fx.mint(), "second grant after DONE, stop and resume"); |
| 422 | assert!( |
| 423 | e.contains("AlreadySpent") && e.contains("settled DONE"), |
| 424 | "{e}" |
| 425 | ); |
| 426 | }); |
| 427 | |
| 428 | linked_test!(no_new_grant_after_done_even_after_revoke, |fx| { |
| 429 | let g = noted(fx.mint()).id; |
| 430 | let i = noted(fx.open(g)).id; |
| 431 | std::fs::write(fx.target(), CONTENT).unwrap(); |
| 432 | assert_eq!(fx.ack_state(i), "DONE"); |
| 433 | controlled(effects::control(&fx.b, "revoke", "drake", Some(g))); |
| 434 | let e = refusal(fx.mint(), "second grant after DONE and revoke"); |
| 435 | assert!( |
| 436 | e.contains("AlreadySpent") && e.contains("settled DONE"), |
| 437 | "{e}" |
| 438 | ); |
| 439 | }); |
| 440 | |
| 441 | // An unsettled intent blocks a new grant even when its own grant was later |
| 442 | // made stale by a stop or revoked: the first write may still land. |
| 443 | linked_test!( |
| 444 | no_new_grant_while_a_stale_or_revoked_grants_intent_is_unsettled, |
| 445 | |fx| { |
| 446 | let g = noted(fx.mint()).id; |
| 447 | noted(fx.open(g)); |
| 448 | controlled(effects::control(&fx.b, "stop", "drake", None)); |
| 449 | controlled(effects::control(&fx.b, "resume", "drake", None)); |
| 450 | let e = refusal( |
| 451 | fx.mint(), |
| 452 | "second grant over an open intent, stop and resume", |
| 453 | ); |
| 454 | assert!(e.contains("ReconciliationRequired"), "{e}"); |
| 455 | controlled(effects::control(&fx.b, "revoke", "drake", Some(g))); |
| 456 | let e = refusal(fx.mint(), "second grant over an open intent, revoked"); |
| 457 | assert!(e.contains("ReconciliationRequired"), "{e}"); |
| 458 | } |
| 459 | ); |
| 460 | |
| 461 | // The one path that must stay open (sc#321 review): a grant whose intent |
| 462 | // settled NOT_DONE (nothing was written) does not block a new grant. |
| 463 | linked_test!( |
| 464 | a_grant_whose_intent_settled_not_done_can_be_authorized_again, |
| 465 | |fx| { |
| 466 | let g = noted(fx.mint()).id; |
| 467 | let i = noted(effects::open_intent(&fx.b, &fx.req(g, false))).id; |
| 468 | let line = effects::reconcile_at_start(&fx.b); |
| 469 | assert!(line.starts_with("Reconcile: checked 1"), "{line}"); |
| 470 | let l = match fx.b.recall(&[], None) { |
| 471 | ControlResponse::ComposeRecalled(r) => effects::Ledger::from_records(&r.host).unwrap(), |
| 472 | other => panic!("{other:?}"), |
| 473 | }; |
| 474 | assert_eq!(l.intents[&i].state.name(), "NOT_DONE"); |
| 475 | let g2 = noted(fx.mint()).id; |
| 476 | assert_ne!(g2, g, "a new, distinct grant"); |
| 477 | assert!(!fx.ws.join("NOTES.md").exists()); |
| 478 | } |
| 479 | ); |
| 480 | |
| 481 | // sovereign-core #323: the NEXT-PHASE-1 effect note (`write_file`, an |
| 482 | // authorization id, no `phase`) spends its authorization in the ledger. Only |
| 483 | // old journals carry it; the daemon writes no such note since NEXT-PHASE-2. |
| 484 | fn legacy_spend(a: u64) -> String { |
| 485 | json!({"tool": "write_file", "authorization": a, "success": true}).to_string() |
| 486 | } |
| 487 | |
| 488 | // Before the fix a caller of ComposeNote could write one naming a live minted |
| 489 | // grant: that grant then refused to execute (AlreadySpent) and a revoke of it |
| 490 | // recorded nothing. A denial, never a second effect, but a forged record all |
| 491 | // the same. |
| 492 | linked_test!(a_caller_cannot_write_a_legacy_spend_note, |fx| { |
| 493 | let g = noted(fx.mint()).id; |
| 494 | let e = refusal( |
| 495 | fx.b.note("effect", &legacy_spend(g), &[]), |
| 496 | "legacy spend note", |
| 497 | ); |
| 498 | assert!(e.contains("write_file"), "{e}"); |
| 499 | // Not even for an id that is no grant (yet). |
| 500 | refusal( |
| 501 | fx.b.note("effect", &legacy_spend(g + 1000), &[]), |
| 502 | "legacy note, future id", |
| 503 | ); |
| 504 | // The grant is untouched: it still executes once. |
| 505 | let i = noted(fx.open(g)).id; |
| 506 | std::fs::write(fx.target(), CONTENT).unwrap(); |
| 507 | assert_eq!(fx.ack_state(i), "DONE"); |
| 508 | // Other caller effect notes are still accepted. |
| 509 | noted(fx.b.note("effect", r#"{"tool":"inspect"}"#, &[])); |
| 510 | }); |
| 511 | |
| 512 | // An old journal that already holds such a note is still read the same way. |
| 513 | linked_test!(an_old_legacy_spend_note_still_spends_its_grant, |fx| { |
| 514 | let g = noted(fx.mint()).id; |
| 515 | noted(fx.b.note_unchecked("effect", &legacy_spend(g), &[])); |
| 516 | let e = refusal(fx.open(g), "grant spent by an old note"); |
| 517 | assert!( |
| 518 | e.contains("AlreadySpent") && e.contains("NEXT-PHASE-1"), |
| 519 | "{e}" |
| 520 | ); |
| 521 | let c = controlled(effects::control(&fx.b, "revoke", "drake", Some(g))); |
| 522 | assert_eq!(c.revoked, Some(false)); |
| 523 | assert!(!Path::new(&fx.target()).exists()); |
| 524 | }); |
| 525 | |
| 526 | // Pin: a legacy note can never hide a DONE effect from the authorize step. |
| 527 | // Every effect has an intent, and the intent is judged first; a legacy mark, |
| 528 | // a stop, a resume or a revoke afterwards changes nothing. |
| 529 | linked_test!( |
| 530 | a_legacy_note_never_lets_a_done_commit_be_authorized_again, |
| 531 | |fx| { |
| 532 | let g = noted(fx.mint()).id; |
| 533 | let i = noted(fx.open(g)).id; |
| 534 | std::fs::write(fx.target(), CONTENT).unwrap(); |
| 535 | assert_eq!(fx.ack_state(i), "DONE"); |
| 536 | noted(fx.b.note_unchecked("effect", &legacy_spend(g), &[])); |
| 537 | let done = |what: &str| { |
| 538 | let e = refusal(fx.mint(), what); |
| 539 | assert!( |
| 540 | e.contains("AlreadySpent") && e.contains("settled DONE"), |
| 541 | "{what}: {e}" |
| 542 | ); |
| 543 | }; |
| 544 | done("after a legacy mark"); |
| 545 | controlled(effects::control(&fx.b, "stop", "drake", None)); |
| 546 | controlled(effects::control(&fx.b, "resume", "drake", None)); |
| 547 | done("after a legacy mark, stop and resume"); |
| 548 | controlled(effects::control(&fx.b, "revoke", "drake", Some(g))); |
| 549 | done("after a legacy mark and revoke"); |
| 550 | assert!(refusal(fx.open(g), "replay").contains("AlreadySpent")); |
| 551 | } |
| 552 | ); |
| 553 | |
| 554 | // A grant marked spent by an old note that never executed, then stopped: one |
| 555 | // new grant may mint (nothing was written), and it gives the one effect. |
| 556 | linked_test!( |
| 557 | a_legacy_marked_unexecuted_grant_still_allows_exactly_one_effect, |
| 558 | |fx| { |
| 559 | let g = noted(fx.mint()).id; |
| 560 | noted(fx.b.note_unchecked("effect", &legacy_spend(g), &[])); |
| 561 | controlled(effects::control(&fx.b, "stop", "drake", None)); |
| 562 | controlled(effects::control(&fx.b, "resume", "drake", None)); |
| 563 | let g2 = noted(fx.mint()).id; |
| 564 | assert!(refusal(fx.open(g), "the legacy-marked grant").contains("AlreadySpent")); |
| 565 | let i = noted(fx.open(g2)).id; |
| 566 | std::fs::write(fx.target(), CONTENT).unwrap(); |
| 567 | assert_eq!(fx.ack_state(i), "DONE"); |
| 568 | let e = refusal(fx.mint(), "a third grant"); |
| 569 | assert!(e.contains("AlreadySpent"), "{e}"); |
| 570 | } |
| 571 | ); |