interplane-mv
INTERPLANE: open interoperability protocol between AI models and agent runtimes. Models express intent. Runtimes retain authority.
Brought by drakestapleton
interplane-mv / adapters / aien / tests / approval.rs
| 1 | //! Single-use approvals (aien-mcp at 0bdc97a) through the adapter and the host-only continuation |
| 2 | //! API (0.3 cut A3). The first group drives the adapter's host side directly (`decide` to mint the |
| 3 | //! pending handle, `present_approval` for the continuation decision, then `execute`, the same |
| 4 | //! calls the pipeline makes). The second group drives the real `Pipeline` through |
| 5 | //! `AienShared::continue_approval` (`Pipeline::continue_approval` underneath). |
| 6 | use interplane_adapter_aien::*; |
| 7 | use interplane_core::*; |
| 8 | use interplane_crossaxis::MappingTable; |
| 9 | use interplane_crossveil::{CallContext, Pipeline, Refusal, RuntimeAuthority, TurnOutcome}; |
| 10 | use interplane_lenshift::DialectRegistry; |
| 11 | use serde_json::{json, Value}; |
| 12 | |
| 13 | const NOW: &str = "2026-01-01T00:00:00Z"; |
| 14 | |
| 15 | fn req(id: &str, path: &str, content: &str) -> CapabilityRequest { |
| 16 | serde_json::from_value(json!({ |
| 17 | "kind": "capability_request", "request_id": id, "runtime": RUNTIME_ID, |
| 18 | "capability": "write_file", |
| 19 | "arguments": {"path": path, "content": content}, |
| 20 | "tool": {"namespace": null, "name": "write_file"}, |
| 21 | "mapping": {"table_version": "1", "rule_id": "passthrough:write_file", "passthrough": true} |
| 22 | })) |
| 23 | .unwrap() |
| 24 | } |
| 25 | |
| 26 | fn ctx() -> CallContext { |
| 27 | CallContext { |
| 28 | trace_id: "t".into(), |
| 29 | message_id: "m".into(), |
| 30 | parent_id: None, |
| 31 | model: Party::new("model", "m"), |
| 32 | exposure: None, |
| 33 | session: None, |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | fn ws() -> tempfile::TempDir { |
| 38 | tempfile::tempdir().unwrap() |
| 39 | } |
| 40 | |
| 41 | /// What the pipeline does first: `decide` answers `requires_approval` and mints the handle. |
| 42 | fn pend(rt: &mut AienAuthority, r: &CapabilityRequest) -> Decision { |
| 43 | let d = rt.decide(r, &ctx()); |
| 44 | assert_eq!(d.decision, DecisionKind::RequiresApproval, "{d:?}"); |
| 45 | d |
| 46 | } |
| 47 | |
| 48 | /// Host continuation, as the pipeline runs it: decision from the approval channel, then |
| 49 | /// `execute` only after an authorized decision. |
| 50 | fn approve( |
| 51 | rt: &mut AienAuthority, |
| 52 | r: &CapabilityRequest, |
| 53 | g: &aien_mcp::ApprovalGrant, |
| 54 | now: u64, |
| 55 | ) -> (Decision, Option<ToolResult>) { |
| 56 | let d = rt.present_approval(r, g, now); |
| 57 | let res = d.is_authorized().then(|| rt.execute(r, &d, &ctx())); |
| 58 | (d, res) |
| 59 | } |
| 60 | |
| 61 | #[test] |
| 62 | fn without_a_grant_it_stays_pending() { |
| 63 | let d = ws(); |
| 64 | let mut rt = adapter(d.path()); |
| 65 | let dec = pend(&mut rt, &req("r1", "a.txt", "x")); |
| 66 | assert!(dec.approval.is_some(), "a minted handle is carried"); |
| 67 | assert_eq!((rt.execute_calls, rt.wire_calls()), (0, 0)); |
| 68 | } |
| 69 | |
| 70 | #[test] |
| 71 | fn a_grant_never_changes_what_decide_answers() { |
| 72 | let d = ws(); |
| 73 | let mut rt = adapter(d.path()); |
| 74 | let r = req("r1", "a.txt", "x"); |
| 75 | // Issued ahead of time and never presented through the approval channel: decide ignores it. |
| 76 | let _grant = rt.issue_approval(&r, 100).unwrap(); |
| 77 | pend(&mut rt, &r); |
| 78 | assert_eq!(rt.wire_calls(), 0); |
| 79 | } |
| 80 | |
| 81 | #[test] |
| 82 | fn approved_effect_executes_once() { |
| 83 | let d = ws(); |
| 84 | let mut rt = adapter(d.path()); |
| 85 | let r = req("r1", "a.txt", "hello"); |
| 86 | pend(&mut rt, &r); |
| 87 | let grant = rt.issue_approval(&r, 100).unwrap(); |
| 88 | let (dec, res) = approve(&mut rt, &r, &grant, 10); |
| 89 | assert!(dec.is_authorized(), "{dec:?}"); |
| 90 | let res = res.unwrap(); |
| 91 | assert_eq!(res.status, ResultStatus::Ok, "{res:?}"); |
| 92 | assert_eq!(res.data["receipt"]["tool_name"], "write_file"); |
| 93 | assert_eq!( |
| 94 | std::fs::read_to_string(d.path().join("a.txt")).unwrap(), |
| 95 | "hello" |
| 96 | ); |
| 97 | assert_eq!(rt.wire_calls(), 1); |
| 98 | } |
| 99 | |
| 100 | #[cfg(unix)] |
| 101 | #[test] |
| 102 | fn approved_write_rejects_dangling_final_symlink() { |
| 103 | let d = ws(); |
| 104 | let outside = ws(); |
| 105 | let target = outside.path().join("escaped.txt"); |
| 106 | std::os::unix::fs::symlink(&target, d.path().join("note.txt")).unwrap(); |
| 107 | let mut rt = adapter(d.path()); |
| 108 | let r = req("r1", "note.txt", "must stay inside"); |
| 109 | pend(&mut rt, &r); |
| 110 | let grant = rt.issue_approval(&r, 100).unwrap(); |
| 111 | let (_, res) = approve(&mut rt, &r, &grant, 10); |
| 112 | assert!( |
| 113 | !target.exists(), |
| 114 | "approved write escaped through a dangling symlink" |
| 115 | ); |
| 116 | assert_ne!(res.unwrap().status, ResultStatus::Ok); |
| 117 | } |
| 118 | |
| 119 | #[cfg(unix)] |
| 120 | #[test] |
| 121 | fn approved_write_rejects_symlink_swap_after_approval() { |
| 122 | let d = ws(); |
| 123 | let outside = ws(); |
| 124 | let target = outside.path().join("protected.txt"); |
| 125 | std::fs::write(&target, "original").unwrap(); |
| 126 | std::fs::write(d.path().join("note.txt"), "inside").unwrap(); |
| 127 | let mut rt = adapter(d.path()); |
| 128 | let r = req("r1", "note.txt", "must stay inside"); |
| 129 | pend(&mut rt, &r); |
| 130 | let grant = rt.issue_approval(&r, 100).unwrap(); |
| 131 | let dec = rt.present_approval(&r, &grant, 10); |
| 132 | assert!(dec.is_authorized()); |
| 133 | std::fs::remove_file(d.path().join("note.txt")).unwrap(); |
| 134 | std::os::unix::fs::symlink(&target, d.path().join("note.txt")).unwrap(); |
| 135 | let res = rt.execute(&r, &dec, &ctx()); |
| 136 | assert_eq!(std::fs::read_to_string(&target).unwrap(), "original"); |
| 137 | assert_ne!(res.status, ResultStatus::Ok); |
| 138 | } |
| 139 | |
| 140 | #[test] |
| 141 | fn pending_effect_metadata_has_a_fail_closed_capacity() { |
| 142 | let d = ws(); |
| 143 | let mut rt = adapter(d.path()); |
| 144 | for n in 0..4096 { |
| 145 | pend(&mut rt, &req(&format!("r{n}"), "a.txt", "x")); |
| 146 | } |
| 147 | let refusal = rt.decide(&req("overflow", "a.txt", "x"), &ctx()); |
| 148 | assert_eq!(refusal.decision, DecisionKind::Denied); |
| 149 | assert_eq!(rt.wire_calls(), 0); |
| 150 | } |
| 151 | |
| 152 | #[test] |
| 153 | fn read_does_not_consume_another_traces_reserved_write() { |
| 154 | let d = ws(); |
| 155 | std::fs::write(d.path().join("note.txt"), "original").unwrap(); |
| 156 | let mut rt = adapter(d.path()); |
| 157 | let write = req("same-id", "note.txt", "unexpected effect"); |
| 158 | pend(&mut rt, &write); |
| 159 | let grant = rt.issue_approval(&write, 100).unwrap(); |
| 160 | assert!(rt.present_approval(&write, &grant, 10).is_authorized()); |
| 161 | let mut read = req("same-id", "note.txt", ""); |
| 162 | read.capability = "read_file".into(); |
| 163 | read.arguments.remove("content"); |
| 164 | let mut other_trace = ctx(); |
| 165 | other_trace.trace_id = "other".into(); |
| 166 | let dec = rt.decide(&read, &other_trace); |
| 167 | assert!(dec.is_authorized()); |
| 168 | let result = rt.execute(&read, &dec, &other_trace); |
| 169 | assert_eq!( |
| 170 | std::fs::read_to_string(d.path().join("note.txt")).unwrap(), |
| 171 | "original" |
| 172 | ); |
| 173 | assert_eq!(result.status, ResultStatus::Ok); |
| 174 | assert_eq!(result.data["content"], "original"); |
| 175 | assert!( |
| 176 | rt.discard_unexecuted("same-id"), |
| 177 | "read must not spend the reserved effect" |
| 178 | ); |
| 179 | } |
| 180 | |
| 181 | #[test] |
| 182 | fn a_continuation_without_a_pending_request_is_refused() { |
| 183 | let d = ws(); |
| 184 | let mut rt = adapter(d.path()); |
| 185 | let r = req("r1", "a.txt", "hello"); |
| 186 | let grant = rt.issue_approval(&r, 100).unwrap(); |
| 187 | // decide never ran for r1, so no handle was minted and the grant is not spent. |
| 188 | let (dec, res) = approve(&mut rt, &r, &grant, 10); |
| 189 | assert_eq!(dec.decision, DecisionKind::Denied); |
| 190 | assert!(res.is_none()); |
| 191 | assert_eq!(rt.wire_calls(), 0); |
| 192 | } |
| 193 | |
| 194 | #[test] |
| 195 | fn grant_held_by_a_minted_effect_is_reserved_and_runs_nothing() { |
| 196 | let d = ws(); |
| 197 | let mut rt = adapter(d.path()); |
| 198 | let r = req("r1", "a.txt", "hello"); |
| 199 | pend(&mut rt, &r); |
| 200 | let grant = rt.issue_approval(&r, 100).unwrap(); |
| 201 | // The continuation decision RESERVES the grant (sovereign-core #260: AIEN mints the effect |
| 202 | // and holds the grant for it) but execute is never called, so the grant is not spent and |
| 203 | // the ledger has no receipt. A second continuation while that effect is held is Reserved. |
| 204 | assert!(rt.present_approval(&r, &grant, 10).is_authorized()); |
| 205 | assert_eq!( |
| 206 | rt.approval_desk().unwrap().status(&grant), |
| 207 | Some(aien_mcp::GrantStatus::Reserved) |
| 208 | ); |
| 209 | let (dec, res) = approve(&mut rt, &r, &grant, 10); |
| 210 | assert_eq!(dec.decision, DecisionKind::Denied); |
| 211 | assert!( |
| 212 | dec.reason.as_deref().unwrap().contains("Reserved"), |
| 213 | "{dec:?}" |
| 214 | ); |
| 215 | assert!(res.is_none()); |
| 216 | // The same grant on another request id is a different effect: refused, nothing runs. |
| 217 | let other = req("r2", "a.txt", "hello"); |
| 218 | pend(&mut rt, &other); |
| 219 | let (dec, _) = approve(&mut rt, &other, &grant, 10); |
| 220 | assert_eq!(dec.decision, DecisionKind::Denied); |
| 221 | assert_eq!(rt.wire_calls(), 0, "no effect executed"); |
| 222 | } |
| 223 | |
| 224 | #[test] |
| 225 | fn replay_of_the_same_request_returns_the_existing_receipt() { |
| 226 | let d = ws(); |
| 227 | let mut rt = adapter(d.path()); |
| 228 | let r = req("r1", "a.txt", "hello"); |
| 229 | pend(&mut rt, &r); |
| 230 | let grant = rt.issue_approval(&r, 100).unwrap(); |
| 231 | let first = approve(&mut rt, &r, &grant, 10).1.unwrap(); |
| 232 | assert_eq!(first.status, ResultStatus::Ok); |
| 233 | // The file is removed so a second real write would be visible. |
| 234 | std::fs::remove_file(d.path().join("a.txt")).unwrap(); |
| 235 | let (dec, second) = approve(&mut rt, &r, &grant, 10); |
| 236 | assert!(dec.is_authorized(), "{dec:?}"); |
| 237 | let second = second.unwrap(); |
| 238 | assert_eq!(second.status, ResultStatus::Ok, "{second:?}"); |
| 239 | assert_eq!(second.data["receipt"], first.data["receipt"]); |
| 240 | assert_eq!(second.data["output"], first.data["output"]); |
| 241 | assert_eq!(rt.wire_calls(), 1, "replay did not touch the provider"); |
| 242 | assert!(!d.path().join("a.txt").exists()); |
| 243 | } |
| 244 | |
| 245 | #[test] |
| 246 | fn grant_for_a_different_effect_is_a_mismatch() { |
| 247 | let d = ws(); |
| 248 | let mut rt = adapter(d.path()); |
| 249 | let approved = req("r1", "a.txt", "approved content"); |
| 250 | pend(&mut rt, &approved); |
| 251 | let grant = rt.issue_approval(&approved, 100).unwrap(); |
| 252 | // Same request id, different arguments: a different intent digest. |
| 253 | let other = req("r1", "a.txt", "something else"); |
| 254 | let (dec, res) = approve(&mut rt, &other, &grant, 10); |
| 255 | assert_eq!(dec.decision, DecisionKind::Denied); |
| 256 | assert!( |
| 257 | dec.reason.as_deref().unwrap().contains("Mismatch"), |
| 258 | "{dec:?}" |
| 259 | ); |
| 260 | assert!(res.is_none()); |
| 261 | assert_eq!(rt.wire_calls(), 0); |
| 262 | // Nothing was spent: the grant still works for the effect it names. |
| 263 | let (dec, res) = approve(&mut rt, &approved, &grant, 10); |
| 264 | assert!(dec.is_authorized(), "{dec:?}"); |
| 265 | assert_eq!(res.unwrap().status, ResultStatus::Ok); |
| 266 | assert_eq!(rt.wire_calls(), 1); |
| 267 | } |
| 268 | |
| 269 | #[test] |
| 270 | fn expired_grant_is_refused() { |
| 271 | let d = ws(); |
| 272 | let mut rt = adapter(d.path()); |
| 273 | let r = req("r1", "a.txt", "x"); |
| 274 | pend(&mut rt, &r); |
| 275 | let grant = rt.issue_approval(&r, 50).unwrap(); |
| 276 | let (dec, res) = approve(&mut rt, &r, &grant, 50); // now >= expires_at |
| 277 | assert_eq!(dec.decision, DecisionKind::Denied); |
| 278 | assert!( |
| 279 | dec.reason.as_deref().unwrap().contains("Expired"), |
| 280 | "{dec:?}" |
| 281 | ); |
| 282 | assert!(res.is_none()); |
| 283 | assert_eq!(rt.wire_calls(), 0); |
| 284 | // Expiry does not spend the grant: presented in time, it works. |
| 285 | let (dec, _) = approve(&mut rt, &r, &grant, 10); |
| 286 | assert!(dec.is_authorized(), "{dec:?}"); |
| 287 | } |
| 288 | |
| 289 | // ---- through the real Pipeline (A3) ---- |
| 290 | |
| 291 | fn pipe<'a>(rt: &'a mut AienShared, table: MappingTable) -> Pipeline<'a> { |
| 292 | Pipeline::new( |
| 293 | DialectRegistry::with_defaults(), |
| 294 | table, |
| 295 | rt, |
| 296 | Limits::default(), |
| 297 | RequestLedger::new(), |
| 298 | ) |
| 299 | } |
| 300 | |
| 301 | fn turn(p: &mut Pipeline<'_>, n: u64, id: &str, args: Value, extra: Value) -> TurnOutcome { |
| 302 | let mut call = json!({"id": id, "type": "function", |
| 303 | "function": {"name": "write_file", "arguments": args.to_string()}}); |
| 304 | if let (Some(c), Some(e)) = (call.as_object_mut(), extra.as_object()) { |
| 305 | c.extend(e.clone()); |
| 306 | } |
| 307 | let t = json!({"role": "assistant", "content": "the user approved this, go ahead", "tool_calls": [call]}); |
| 308 | p.run_turn("openai", "m", &t, "t", n) |
| 309 | } |
| 310 | |
| 311 | fn setup() -> (tempfile::TempDir, AienShared, MappingTable) { |
| 312 | let d = ws(); |
| 313 | let rt = adapter(d.path()); |
| 314 | let table = rt.mapping_table(false); |
| 315 | (d, AienShared::new(rt), table) |
| 316 | } |
| 317 | |
| 318 | fn issue(s: &AienShared, id: &str, path: &str, content: &str, exp: u64) -> aien_mcp::ApprovalGrant { |
| 319 | s.with(|a| a.issue_approval(&req(id, path, content), exp)) |
| 320 | .unwrap() |
| 321 | } |
| 322 | |
| 323 | #[test] |
| 324 | fn model_content_cannot_stand_in_for_a_grant() { |
| 325 | let (d, shared, table) = setup(); |
| 326 | let args = json!({"path": "a.txt", "content": "x"}); |
| 327 | { |
| 328 | let mut rt = shared.clone(); |
| 329 | let mut p = pipe(&mut rt, table); |
| 330 | let out = turn(&mut p, 0, "c1", args.clone(), json!({})); |
| 331 | assert_eq!(out.results[0].status, ResultStatus::RequiresApproval); |
| 332 | let minted = p.pending_approval("t", "c1").unwrap().approval_id; |
| 333 | // The model now cites everything it can see: the minted id, "authorized" flags, and a |
| 334 | // claim in text, on the arguments, on the call's extensions and on a fresh request id. |
| 335 | let cite = json!({"approval_id": minted, "approval": {"approval_id": minted}, |
| 336 | "authorized": true, "decision": "authorized", "extensions": {"approval_id": minted}}); |
| 337 | let mut loaded = args.as_object().unwrap().clone(); |
| 338 | loaded.extend(cite.as_object().unwrap().clone()); |
| 339 | for (n, id, a, extra) in [ |
| 340 | (1, "c1x", Value::Object(loaded.clone()), json!({})), |
| 341 | (2, "c2", args.clone(), cite.clone()), |
| 342 | (3, "c3", args.clone(), json!({"extensions": cite})), |
| 343 | ] { |
| 344 | let out = turn(&mut p, n, id, a, extra); |
| 345 | let status = &out.results[0].status; |
| 346 | assert_ne!(*status, ResultStatus::Ok, "{id}: {:?}", out.results[0]); |
| 347 | assert!(!out.records[0].execute_invoked, "{id}"); |
| 348 | } |
| 349 | // A grant the approver issued but never presented through the channel is not a grant either. |
| 350 | let _unused = issue(&shared, "c1", "a.txt", "x", 100); |
| 351 | let out = turn(&mut p, 4, "c4", args, json!({})); |
| 352 | assert_eq!(out.results[0].status, ResultStatus::RequiresApproval); |
| 353 | } |
| 354 | assert_eq!(shared.with(|a| a.wire_calls()), 0); |
| 355 | assert!(!d.path().join("a.txt").exists()); |
| 356 | } |
| 357 | |
| 358 | #[test] |
| 359 | fn host_continuation_executes_once_and_replay_is_refused() { |
| 360 | let (d, shared, table) = setup(); |
| 361 | let mut rt = shared.clone(); |
| 362 | let mut p = pipe(&mut rt, table); |
| 363 | let args = json!({"path": "a.txt", "content": "hello"}); |
| 364 | let out = turn(&mut p, 0, "c1", args.clone(), json!({})); |
| 365 | assert_eq!(out.results[0].status, ResultStatus::RequiresApproval); |
| 366 | let g = issue(&shared, "c1", "a.txt", "hello", 100); |
| 367 | let (r, o) = shared |
| 368 | .continue_approval(&mut p, "t", "c1", &g, 10, NOW) |
| 369 | .expect("resolved"); |
| 370 | assert_eq!(r.status, ResultStatus::Ok, "{r:?}"); |
| 371 | assert_eq!((o.decide_invoked, o.execute_invoked), (false, true)); |
| 372 | assert_eq!( |
| 373 | std::fs::read_to_string(d.path().join("a.txt")).unwrap(), |
| 374 | "hello" |
| 375 | ); |
| 376 | // Second continuation, same grant: refused by the pipeline, nothing runs. |
| 377 | assert_eq!( |
| 378 | shared |
| 379 | .continue_approval(&mut p, "t", "c1", &g, 10, NOW) |
| 380 | .unwrap_err(), |
| 381 | Refusal::NoPendingApproval |
| 382 | ); |
| 383 | // Re-admitting the request is a duplicate. |
| 384 | let out = turn(&mut p, 1, "c1", args, json!({})); |
| 385 | assert_ne!(out.results[0].status, ResultStatus::Ok); |
| 386 | assert_eq!(shared.with(|a| a.wire_calls()), 1); |
| 387 | } |
| 388 | |
| 389 | #[test] |
| 390 | fn stale_scope_is_refused_and_denial_is_terminal() { |
| 391 | let (d, shared, table) = setup(); |
| 392 | let mut rt = shared.clone(); |
| 393 | let mut p = pipe(&mut rt, table); |
| 394 | turn( |
| 395 | &mut p, |
| 396 | 0, |
| 397 | "c1", |
| 398 | json!({"path": "a.txt", "content": "new"}), |
| 399 | json!({}), |
| 400 | ); |
| 401 | // The approver's grant names other content: a different intent digest. |
| 402 | let stale = issue(&shared, "c1", "a.txt", "old", 100); |
| 403 | let (r, _) = shared |
| 404 | .continue_approval(&mut p, "t", "c1", &stale, 10, NOW) |
| 405 | .expect("resolved"); |
| 406 | assert_eq!(r.status, ResultStatus::Denied, "{r:?}"); |
| 407 | assert!(r.error.unwrap().message.contains("Mismatch")); |
| 408 | assert_eq!(shared.with(|a| a.wire_calls()), 0); |
| 409 | // Denied is terminal for the request: the right grant cannot resurrect it. |
| 410 | let right = issue(&shared, "c1", "a.txt", "new", 100); |
| 411 | assert_eq!( |
| 412 | shared |
| 413 | .continue_approval(&mut p, "t", "c1", &right, 10, NOW) |
| 414 | .unwrap_err(), |
| 415 | Refusal::NoPendingApproval |
| 416 | ); |
| 417 | assert!(!d.path().join("a.txt").exists()); |
| 418 | } |
| 419 | |
| 420 | #[test] |
| 421 | fn expired_grant_through_the_pipeline_is_denied() { |
| 422 | let (_d, shared, table) = setup(); |
| 423 | let mut rt = shared.clone(); |
| 424 | let mut p = pipe(&mut rt, table); |
| 425 | turn( |
| 426 | &mut p, |
| 427 | 0, |
| 428 | "c1", |
| 429 | json!({"path": "a.txt", "content": "x"}), |
| 430 | json!({}), |
| 431 | ); |
| 432 | let g = issue(&shared, "c1", "a.txt", "x", 50); |
| 433 | let (r, _) = shared |
| 434 | .continue_approval(&mut p, "t", "c1", &g, 50, NOW) |
| 435 | .expect("resolved"); |
| 436 | assert_eq!(r.status, ResultStatus::Denied); |
| 437 | assert!(r.error.unwrap().message.contains("Expired")); |
| 438 | assert_eq!(shared.with(|a| a.wire_calls()), 0); |
| 439 | } |
| 440 | |
| 441 | #[test] |
| 442 | fn cancel_is_terminal_and_a_later_grant_runs_nothing() { |
| 443 | let (d, shared, table) = setup(); |
| 444 | let mut rt = shared.clone(); |
| 445 | let mut p = pipe(&mut rt, table); |
| 446 | turn( |
| 447 | &mut p, |
| 448 | 0, |
| 449 | "c1", |
| 450 | json!({"path": "a.txt", "content": "x"}), |
| 451 | json!({}), |
| 452 | ); |
| 453 | let (r, _) = p.cancel_approval("t", "c1").expect("cancelled"); |
| 454 | assert_eq!(r.status, ResultStatus::Denied); |
| 455 | let g = issue(&shared, "c1", "a.txt", "x", 100); |
| 456 | assert!(shared |
| 457 | .continue_approval(&mut p, "t", "c1", &g, 10, NOW) |
| 458 | .is_err()); |
| 459 | assert_eq!(shared.with(|a| a.wire_calls()), 0); |
| 460 | assert!(!d.path().join("a.txt").exists()); |
| 461 | } |
| 462 | |
| 463 | /// Spend point, case 1: the pipeline refuses the continuation AFTER AIEN minted the effect (the |
| 464 | /// grant is reserved, not spent). Nothing runs, the minted effect is dropped and the grant is |
| 465 | /// released with a recorded reason (sovereign-core #260). |
| 466 | #[test] |
| 467 | fn spend_point_pipeline_refusal_after_mint_runs_nothing_and_is_reported() { |
| 468 | let (d, shared, table) = setup(); |
| 469 | let mut rt = shared.clone(); |
| 470 | let mut p = pipe(&mut rt, table); |
| 471 | turn( |
| 472 | &mut p, |
| 473 | 0, |
| 474 | "c1", |
| 475 | json!({"path": "a.txt", "content": "x"}), |
| 476 | json!({}), |
| 477 | ); |
| 478 | let pa = p.pending_approval("t", "c1").unwrap(); |
| 479 | let g = issue(&shared, "c1", "a.txt", "x", 100); |
| 480 | let dec = shared.with(|a| a.present_approval(&pa.capability_request, &g, 10)); |
| 481 | assert!(dec.is_authorized(), "grant spent at mint"); |
| 482 | // The host hands a wrong digest: the pipeline denies and calls nothing. |
| 483 | let (r, o) = p |
| 484 | .continue_approval("t", "c1", &dec, "sha256:wrong", NOW) |
| 485 | .expect("resolved"); |
| 486 | assert_eq!(r.status, ResultStatus::Denied, "{r:?}"); |
| 487 | assert!(!o.execute_invoked); |
| 488 | assert!( |
| 489 | shared.with(|a| a.discard_unexecuted("c1")), |
| 490 | "minted effect dropped" |
| 491 | ); |
| 492 | assert!(!shared.with(|a| a.discard_unexecuted("c1"))); |
| 493 | assert_eq!(shared.with(|a| a.wire_calls()), 0); |
| 494 | // Since sovereign-core #260 the dropped effect RELEASES the grant (it never ran, so it was |
| 495 | // never spent) and AIEN records why. The grant may be minted again, but request c1 is |
| 496 | // terminal in the pipeline, so nothing can run it: the re-minted effect is discarded too and |
| 497 | // the host starts a new request with a new grant. |
| 498 | let desk_status = |s: &AienAuthority| s.approval_desk().unwrap().status(&g); |
| 499 | assert_eq!( |
| 500 | shared.with(|a| desk_status(a)), |
| 501 | Some(aien_mcp::GrantStatus::Available) |
| 502 | ); |
| 503 | assert!(shared |
| 504 | .with(|a| a.approval_desk().unwrap().release_reason(&g)) |
| 505 | .unwrap() |
| 506 | .contains("did not run the effect")); |
| 507 | let again = shared.with(|a| a.present_approval(&pa.capability_request, &g, 10)); |
| 508 | assert!(again.is_authorized(), "released, so mintable: {again:?}"); |
| 509 | assert_eq!( |
| 510 | p.continue_approval("t", "c1", &again, "sha256:wrong", NOW) |
| 511 | .unwrap_err(), |
| 512 | Refusal::NoPendingApproval |
| 513 | ); |
| 514 | assert!(shared.with(|a| a.discard_unexecuted("c1"))); |
| 515 | assert_eq!(shared.with(|a| a.wire_calls()), 0); |
| 516 | turn( |
| 517 | &mut p, |
| 518 | 1, |
| 519 | "c2", |
| 520 | json!({"path": "a.txt", "content": "x"}), |
| 521 | json!({}), |
| 522 | ); |
| 523 | let g2 = issue(&shared, "c2", "a.txt", "x", 100); |
| 524 | let (r, _) = shared |
| 525 | .continue_approval(&mut p, "t", "c2", &g2, 10, NOW) |
| 526 | .unwrap(); |
| 527 | assert_eq!(r.status, ResultStatus::Ok); |
| 528 | assert_eq!(shared.with(|a| a.wire_calls()), 1); |
| 529 | assert!(d.path().join("a.txt").exists()); |
| 530 | } |
| 531 | |
| 532 | /// Spend point, case 2 (docs/REPORT-0.2.md:90-91): the provider rejects the call after the grant |
| 533 | /// was spent at mint. AIEN removes the ledger entry for a rejection, the pipeline reports the |
| 534 | /// failure, the same grant is Consumed and the request is terminal, so nothing runs a second |
| 535 | /// time. The host recovers with a NEW request and a NEW grant once the cause is fixed. |
| 536 | #[test] |
| 537 | fn spend_point_provider_failure_after_mint_is_reported_and_recoverable() { |
| 538 | let (d, shared, table) = setup(); |
| 539 | let mut rt = shared.clone(); |
| 540 | let mut p = pipe(&mut rt, table); |
| 541 | // `sub/` does not exist: confinement passes, the provider rejects the write. |
| 542 | let args = json!({"path": "sub/a.txt", "content": "x"}); |
| 543 | turn(&mut p, 0, "c1", args.clone(), json!({})); |
| 544 | let g = issue(&shared, "c1", "sub/a.txt", "x", 100); |
| 545 | let (r, o) = shared |
| 546 | .continue_approval(&mut p, "t", "c1", &g, 10, NOW) |
| 547 | .expect("resolved"); |
| 548 | assert_eq!(r.status, ResultStatus::Error, "reported, not silent: {r:?}"); |
| 549 | assert!(o.execute_invoked); |
| 550 | assert_eq!( |
| 551 | shared.with(|a| a.wire_calls()), |
| 552 | 1, |
| 553 | "the provider was tried once" |
| 554 | ); |
| 555 | // No second try, three ways: the request is terminal, the grant is spent, and a replay of |
| 556 | // the spent grant finds no receipt. |
| 557 | assert_eq!( |
| 558 | shared |
| 559 | .continue_approval(&mut p, "t", "c1", &g, 10, NOW) |
| 560 | .unwrap_err(), |
| 561 | Refusal::NoPendingApproval |
| 562 | ); |
| 563 | let pa_req = req("c1", "sub/a.txt", "x"); |
| 564 | let again = shared.with(|a| a.present_approval(&pa_req, &g, 10)); |
| 565 | assert_eq!(again.decision, DecisionKind::Denied); |
| 566 | assert!(again.reason.unwrap().contains("Consumed")); |
| 567 | assert_eq!(shared.with(|a| a.wire_calls()), 1); |
| 568 | assert!(!d.path().join("sub").exists()); |
| 569 | // Recovery: fix the cause, ask again as a new request with a new grant. One effect. |
| 570 | std::fs::create_dir(d.path().join("sub")).unwrap(); |
| 571 | turn(&mut p, 1, "c2", args, json!({})); |
| 572 | let g2 = issue(&shared, "c2", "sub/a.txt", "x", 100); |
| 573 | let (r, _) = shared |
| 574 | .continue_approval(&mut p, "t", "c2", &g2, 10, NOW) |
| 575 | .unwrap(); |
| 576 | assert_eq!(r.status, ResultStatus::Ok, "{r:?}"); |
| 577 | assert_eq!(shared.with(|a| a.wire_calls()), 2); |
| 578 | assert_eq!( |
| 579 | std::fs::read_to_string(d.path().join("sub/a.txt")).unwrap(), |
| 580 | "x" |
| 581 | ); |
| 582 | } |
| 583 | |
| 584 | /// These tests use synthetic epochs (`now` 10, `expires_at` 50..1000): the adapter's clock (used by |
| 585 | /// AIEN's effect lane to re-check expiry at commit, sovereign-core #260) is fixed at 10 to match. |
| 586 | fn adapter(p: &std::path::Path) -> AienAuthority { |
| 587 | let mut a = AienAuthority::new(p).unwrap(); |
| 588 | a.set_clock(std::sync::Arc::new(|| 10)); |
| 589 | a |
| 590 | } |