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 / approved_proposal_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 | //! Proposer hook (crate::approved): an approved proposal goes through the |
| 17 | //! production compose run (J-Space branch, AEGIS verify, World commit) and |
| 18 | //! then the durable effect ledger; unverified, unauthenticated (#249 A) and |
| 19 | //! replayed (#249 B) proposals are refused with no compose run. CPU only, no |
| 20 | //! model, no socket: the bridge is driven exactly as the daemon drives it. In |
| 21 | //! a stub build only the refusal paths run. |
| 22 | #[path = "support/home_guard.rs"] |
| 23 | mod home_guard; |
| 24 | |
| 25 | use aien_omega_compose::hex; |
| 26 | use aien_runtime::approved::{ |
| 27 | self, ApprovedComposeReport, ApprovedProposal, ApprovedRefusal, ProposerHook, |
| 28 | }; |
| 29 | use aien_runtime::approved_auth::{ |
| 30 | approval_key, canonical_workspace, desk_key_path, ApprovalIdentity, DeskKey, |
| 31 | }; |
| 32 | use aien_runtime::control::{ComposeRecallReport, ControlResponse}; |
| 33 | use aien_runtime::effects::{self, IntentRequest, Ledger}; |
| 34 | use aien_runtime::{ComposeBridge, ComposeProposer}; |
| 35 | use serde_json::{json, Value}; |
| 36 | use sha2::{Digest, Sha256}; |
| 37 | use std::path::Path; |
| 38 | use std::sync::Arc; |
| 39 | |
| 40 | /// The proposer label of a run whose proposal came from the approved-proposal hook. |
| 41 | const APPROVED_PROPOSER_LABEL: &str = "hook:approved-proposal"; |
| 42 | |
| 43 | const PATH: &str = "NOTES.md"; |
| 44 | const CONTENT: &str = "approved by the INTERPLANE desk\n"; |
| 45 | |
| 46 | /// The model proposer must never be called on the hook path. |
| 47 | fn model_never_called() -> ComposeProposer { |
| 48 | Arc::new(|_: &str, _: std::time::Duration| panic!("the model proposer ran on the hook path")) |
| 49 | } |
| 50 | |
| 51 | fn sha(b: &[u8]) -> String { |
| 52 | hex(&Sha256::digest(b)) |
| 53 | } |
| 54 | |
| 55 | /// Independent of approved::approved_proposal_sha256: the interplane#77 |
| 56 | /// canonical form written out by hand. |
| 57 | fn interplane_sha(path: &str, content: &str) -> String { |
| 58 | let s = format!( |
| 59 | "{{\"content\":{},\"path\":{}}}", |
| 60 | serde_json::to_string(content).unwrap(), |
| 61 | serde_json::to_string(path).unwrap() |
| 62 | ); |
| 63 | sha(s.as_bytes()) |
| 64 | } |
| 65 | |
| 66 | /// Unsigned: tests sign with [`sign`] after any edit. |
| 67 | fn unsigned(request: &str, approval: &str) -> ApprovedProposal { |
| 68 | ApprovedProposal { |
| 69 | request_id: request.into(), |
| 70 | trace_id: format!("trace-{request}"), |
| 71 | approval_id: approval.into(), |
| 72 | approver: "interplane-host".into(), |
| 73 | path: PATH.into(), |
| 74 | content: CONTENT.into(), |
| 75 | approved_proposal_sha256: interplane_sha(PATH, CONTENT), |
| 76 | content_sha256: sha(CONTENT.as_bytes()), |
| 77 | approval_mac: String::new(), |
| 78 | requirements: Some(String::new()), |
| 79 | requirements_mac: String::new(), |
| 80 | requirements_base: None, |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | /// The desk key of a compose home (the test is the desk). |
| 85 | fn desk(b: &ComposeBridge) -> DeskKey { |
| 86 | DeskKey::load(&desk_key_path(b.dir())).unwrap() |
| 87 | } |
| 88 | |
| 89 | /// The workspace every test home pairs with its compose home (`setup`: |
| 90 | /// `<tmp>/compose` and `<tmp>/ws`); the approval binds it. |
| 91 | fn ws_of(b: &ComposeBridge) -> std::path::PathBuf { |
| 92 | b.dir().parent().unwrap().join("ws") |
| 93 | } |
| 94 | |
| 95 | fn sign(b: &ComposeBridge, mut p: ApprovedProposal) -> ApprovedProposal { |
| 96 | desk(b).seal(&mut p, &ws_of(b)); |
| 97 | p |
| 98 | } |
| 99 | |
| 100 | /// Re-sign after editing fields (an honest desk approving the edited change). |
| 101 | fn resign(b: &ComposeBridge, p: &ApprovedProposal) -> ApprovedProposal { |
| 102 | sign(b, p.clone()) |
| 103 | } |
| 104 | |
| 105 | fn recalled(b: &ComposeBridge, ids: &[u64]) -> ComposeRecallReport { |
| 106 | match b.recall(ids, None) { |
| 107 | ControlResponse::ComposeRecalled(r) => *r, |
| 108 | other => panic!("recall: {other:?}"), |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | fn refused( |
| 113 | r: Result<ApprovedComposeReport, Box<ApprovedRefusal>>, |
| 114 | name: &str, |
| 115 | ) -> Box<ApprovedRefusal> { |
| 116 | match r { |
| 117 | Err(e) => { |
| 118 | assert_eq!(e.name, name, "want {name}, got {e}"); |
| 119 | e |
| 120 | } |
| 121 | Ok(v) => panic!("want refusal {name}, got {v:?}"), |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | /// Stub build: nothing opens the compose home, so the claim is refused. |
| 126 | fn stub_refused(r: Result<ApprovedComposeReport, Box<ApprovedRefusal>>) { |
| 127 | let e = r.expect_err("stub build must refuse"); |
| 128 | assert!( |
| 129 | e.refused_by == "REPLAY_REFUSED" || e.name == "ComposeError", |
| 130 | "{e}" |
| 131 | ); |
| 132 | } |
| 133 | |
| 134 | fn committed(r: Result<ApprovedComposeReport, Box<ApprovedRefusal>>) -> ApprovedComposeReport { |
| 135 | let r = r.unwrap_or_else(|e| panic!("want COMMITTED, got {e}")); |
| 136 | assert_eq!(r.state, "COMMITTED"); |
| 137 | r |
| 138 | } |
| 139 | |
| 140 | /// omega caps the compose homes one process may hold open (E_FULL): the |
| 141 | /// tests of this binary take turns. |
| 142 | static HOMES: std::sync::Mutex<()> = std::sync::Mutex::new(()); |
| 143 | |
| 144 | fn turn() -> std::sync::MutexGuard<'static, ()> { |
| 145 | HOMES.lock().unwrap_or_else(|e| e.into_inner()) |
| 146 | } |
| 147 | |
| 148 | fn setup() -> (tempfile::TempDir, Arc<ComposeBridge>, ProposerHook, String) { |
| 149 | let tmp = tempfile::tempdir().unwrap(); |
| 150 | let ws = tmp.path().join("ws"); |
| 151 | std::fs::create_dir_all(&ws).unwrap(); |
| 152 | let b = Arc::new(ComposeBridge::new( |
| 153 | tmp.path().join("compose"), |
| 154 | model_never_called(), |
| 155 | "test:model-never-called", |
| 156 | )); |
| 157 | DeskKey::create(&desk_key_path(b.dir())).unwrap(); |
| 158 | let hook = ProposerHook::new(b.clone()); |
| 159 | let ws = ws.display().to_string(); |
| 160 | (tmp, b, hook, ws) |
| 161 | } |
| 162 | |
| 163 | #[test] |
| 164 | #[cfg_attr(not(compose_linked), ignore = "needs librx_compose.a: stub build")] |
| 165 | fn approved_proposal_commits_then_effect_is_done() { |
| 166 | let _home = home_guard::home_slot(); |
| 167 | let _turn = turn(); |
| 168 | let (_tmp, b, hook, ws) = setup(); |
| 169 | let p = sign(&b, unsigned("req-1", "appr-1")); |
| 170 | let out = hook.submit(&p, &ws); |
| 171 | if !aien_omega_compose::LINKED { |
| 172 | stub_refused(out); |
| 173 | return; |
| 174 | } |
| 175 | let r = committed(out); |
| 176 | // Correlation unchanged, authentication and replay identity reported. |
| 177 | assert_eq!( |
| 178 | ( |
| 179 | r.request_id.as_str(), |
| 180 | r.trace_id.as_str(), |
| 181 | r.approval_id.as_str() |
| 182 | ), |
| 183 | ("req-1", "trace-req-1", "appr-1") |
| 184 | ); |
| 185 | let d = desk(&b); |
| 186 | assert_eq!(r.desk_key_id, d.id()); |
| 187 | assert_eq!( |
| 188 | r.approval_key, |
| 189 | approval_key(&ApprovalIdentity::of( |
| 190 | &p, |
| 191 | &canonical_workspace(&ws_of(&b)).unwrap(), |
| 192 | d.id() |
| 193 | )) |
| 194 | ); |
| 195 | assert!(r.replay_claim != 0); |
| 196 | assert_eq!(r.approved_proposal_sha256, interplane_sha(PATH, CONTENT)); |
| 197 | let text = format!("filename: {PATH}\n{CONTENT}"); |
| 198 | assert_eq!(r.compose_proposal_sha256, sha(text.as_bytes())); |
| 199 | assert_ne!(r.approved_proposal_sha256, r.compose_proposal_sha256); |
| 200 | // The compose run: one J-Space branch, AEGIS pass, World commit. |
| 201 | let t = r.task.as_ref().unwrap(); |
| 202 | assert!(t.committed, "{t:?}"); |
| 203 | assert_eq!((t.outcome, t.branch_count, t.winner), (1, 1, Some(0))); |
| 204 | assert_eq!(t.aegis_pass_mask & 1, 1); |
| 205 | assert_eq!(t.proposer, APPROVED_PROPOSER_LABEL); |
| 206 | assert_eq!(t.proposal_attempts.len(), 1); |
| 207 | assert_eq!(t.proposal_attempts[0].aegis.as_deref(), Some("pass")); |
| 208 | assert!(t.cx_goal != 0 && t.cx_evidence != 0 && t.cx_promotion != 0); |
| 209 | assert_eq!(r.grant_links, vec![t.cx_promotion, t.cx_evidence]); |
| 210 | // The World commit record is in the Cortex journal and verifies. |
| 211 | let c = recalled(&b, &[t.cx_promotion]); |
| 212 | assert!(c.missing.is_empty(), "{c:?}"); |
| 213 | assert!(c.cited[0].verified, "{:?}", c.cited[0]); |
| 214 | assert_eq!(c.cited[0].id, t.cx_promotion); |
| 215 | // Nothing touched the workspace yet: the hook grants nothing. |
| 216 | let target = Path::new(&ws).join(PATH); |
| 217 | assert!(!target.exists()); |
| 218 | |
| 219 | // Effect: the daemon's own grant (reserved, keyed on the compose hash, |
| 220 | // linked to promotion, evidence and the replay claim), intent, write, ack. |
| 221 | let a = r.approved_grant.expect("the daemon wrote the grant"); |
| 222 | let tgt = r.target.clone().expect("grant target"); |
| 223 | assert_eq!( |
| 224 | Path::new(&tgt), |
| 225 | std::fs::canonicalize(&ws).unwrap().join(PATH) |
| 226 | ); |
| 227 | let g = recalled(&b, &[a]); |
| 228 | assert!(g.cited[0].verified); |
| 229 | assert_eq!(g.cited[0].note.as_deref(), Some("authorization")); |
| 230 | for x in [t.cx_promotion, t.cx_evidence, r.replay_claim] { |
| 231 | assert!(g.cited[0].links.contains(&x), "{:?}", g.cited[0]); |
| 232 | } |
| 233 | let gv: Value = serde_json::from_str(g.cited[0].text.as_deref().unwrap()).unwrap(); |
| 234 | assert_eq!(gv["approved_grant"], 1); |
| 235 | assert_eq!(gv["proposal_sha256"], json!(r.compose_proposal_sha256)); |
| 236 | assert_eq!( |
| 237 | gv["approved_proposal_sha256"], |
| 238 | json!(r.approved_proposal_sha256) |
| 239 | ); |
| 240 | assert_eq!(gv["approval_key"], json!(r.approval_key)); |
| 241 | assert_eq!( |
| 242 | (gv["request_id"].as_str(), gv["trace_id"].as_str()), |
| 243 | (Some(r.request_id.as_str()), Some(r.trace_id.as_str())) |
| 244 | ); |
| 245 | assert_eq!(gv["prior_sha256"], Value::Null); |
| 246 | let target = Path::new(&tgt).to_path_buf(); |
| 247 | let (pid, start) = effects::self_executor(); |
| 248 | let req = IntentRequest { |
| 249 | authorization: a, |
| 250 | proposal_sha256: r.compose_proposal_sha256.clone(), |
| 251 | path: r.path.clone(), |
| 252 | target: tgt.clone(), |
| 253 | content_sha256: r.content_sha256.clone(), |
| 254 | executor_pid: pid, |
| 255 | executor_start: start, |
| 256 | }; |
| 257 | let i = match effects::open_intent(&b, &req) { |
| 258 | ControlResponse::ComposeNoted(n) => n.id, |
| 259 | other => panic!("intent: {other:?}"), |
| 260 | }; |
| 261 | std::fs::write(&target, CONTENT).unwrap(); |
| 262 | let k = match effects::ack(&b, i, &json!({"written_sha256": r.content_sha256})) { |
| 263 | ControlResponse::ComposeNoted(n) => n.id, |
| 264 | other => panic!("ack: {other:?}"), |
| 265 | }; |
| 266 | let rec = recalled(&b, &[k]); |
| 267 | let v: Value = serde_json::from_str(rec.cited[0].text.as_deref().unwrap()).unwrap(); |
| 268 | assert_eq!(v["state"], "DONE", "{v}"); |
| 269 | assert_eq!(std::fs::read(&target).unwrap(), CONTENT.as_bytes()); |
| 270 | } |
| 271 | |
| 272 | #[test] |
| 273 | fn unverified_proposal_is_refused_with_zero_effects() { |
| 274 | let _home = home_guard::home_slot(); |
| 275 | let _turn = turn(); |
| 276 | let (_tmp, b, hook, ws) = setup(); |
| 277 | println!( |
| 278 | "approved_proposal_test: compose LINKED={} (zero-journal assertions {})", |
| 279 | aien_omega_compose::LINKED, |
| 280 | if aien_omega_compose::LINKED { |
| 281 | "run" |
| 282 | } else { |
| 283 | "skipped: stub build" |
| 284 | } |
| 285 | ); |
| 286 | let before = aien_omega_compose::LINKED.then(|| recalled(&b, &[]).records_total); |
| 287 | let mut cases: Vec<(&str, ApprovedProposal)> = Vec::new(); |
| 288 | let proposal = |r: &str, a: &str| unsigned(r, a); |
| 289 | let mut p = proposal("u1", "a1"); |
| 290 | p.content_sha256 = sha(b"other bytes\n"); |
| 291 | cases.push(("content sha mismatch", p)); |
| 292 | let mut p = proposal("u2", "a2"); |
| 293 | // The compose text sha in the approved slot: the other hash semantics. |
| 294 | p.approved_proposal_sha256 = sha(format!("filename: {PATH}\n{CONTENT}").as_bytes()); |
| 295 | cases.push(("text sha in the approved slot", p)); |
| 296 | let mut p = proposal("u3", "a3"); |
| 297 | p.path = "../escape.md".into(); |
| 298 | p.approved_proposal_sha256 = interplane_sha(&p.path, CONTENT); |
| 299 | cases.push(("path outside the workspace", p)); |
| 300 | let mut p = proposal("u4", "a4"); |
| 301 | p.content = "no trailing newline".into(); |
| 302 | p.content_sha256 = sha(p.content.as_bytes()); |
| 303 | p.approved_proposal_sha256 = interplane_sha(PATH, &p.content); |
| 304 | cases.push(("content the template would change", p)); |
| 305 | let mut p = proposal("u5", ""); |
| 306 | p.approval_id = " ".into(); |
| 307 | cases.push(("no approval id", p)); |
| 308 | let mut p = proposal("", "a6"); |
| 309 | p.request_id = String::new(); |
| 310 | cases.push(("no request id", p)); |
| 311 | for (what, p) in &cases { |
| 312 | // Signed by the desk: verification still refuses. |
| 313 | let r = hook.submit(&resign(&b, p), &ws); |
| 314 | assert!(r.is_err(), "{what}: {r:?}"); |
| 315 | refused(r, "Unverified"); |
| 316 | } |
| 317 | assert!(!Path::new(&ws).join(PATH).exists()); |
| 318 | assert!(!Path::new(&ws).join("../escape.md").exists()); |
| 319 | if let Some(n) = before { |
| 320 | let after = recalled(&b, &[]); |
| 321 | assert_eq!( |
| 322 | after.records_total, n, |
| 323 | "a refused proposal appended records" |
| 324 | ); |
| 325 | let l = Ledger::from_records(&after.host).unwrap(); |
| 326 | assert!(l.grants.is_empty() && l.intents.is_empty()); |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | /// Replay in the same process: the same approval again returns the original |
| 331 | /// result without a second run (ALREADY_COMMITTED); the request id or the |
| 332 | /// approval id under another approval is refused; nothing is appended. |
| 333 | #[test] |
| 334 | #[cfg_attr(not(compose_linked), ignore = "needs librx_compose.a: stub build")] |
| 335 | fn duplicate_proposal_is_refused() { |
| 336 | let _home = home_guard::home_slot(); |
| 337 | let _turn = turn(); |
| 338 | let (_tmp, b, hook, ws) = setup(); |
| 339 | let p = sign(&b, unsigned("req-d", "appr-d")); |
| 340 | let first = hook.submit(&p, &ws); |
| 341 | if !aien_omega_compose::LINKED { |
| 342 | stub_refused(first); |
| 343 | stub_refused(hook.submit(&p, &ws)); |
| 344 | return; |
| 345 | } |
| 346 | let first = committed(first); |
| 347 | let n = recalled(&b, &[]).records_total; |
| 348 | let again = hook.submit(&p, &ws).unwrap(); |
| 349 | assert_eq!(again.state, "ALREADY_COMMITTED"); |
| 350 | assert!(again.task.is_none()); |
| 351 | assert_eq!(again.compose_proposal_sha256, first.compose_proposal_sha256); |
| 352 | assert_eq!(again.grant_links, first.grant_links); |
| 353 | assert_eq!(again.replay_claim, first.replay_claim); |
| 354 | // Same approval id, new request (desk-signed): refused, not "already committed". |
| 355 | let e = refused( |
| 356 | hook.submit(&sign(&b, unsigned("req-other", "appr-d")), &ws), |
| 357 | "AlreadyCommitted", |
| 358 | ); |
| 359 | assert_eq!(e.refused_by, "REPLAY_REFUSED"); |
| 360 | assert_eq!(e.replay_claim, Some(first.replay_claim)); |
| 361 | refused( |
| 362 | hook.submit(&sign(&b, unsigned("req-d", "appr-other")), &ws), |
| 363 | "AlreadyCommitted", |
| 364 | ); |
| 365 | // Same ids, another trace: refused too. |
| 366 | let mut t = unsigned("req-d", "appr-d"); |
| 367 | t.trace_id = "trace-other".into(); |
| 368 | refused(hook.submit(&sign(&b, t), &ws), "AlreadyCommitted"); |
| 369 | assert_eq!( |
| 370 | recalled(&b, &[]).records_total, |
| 371 | n, |
| 372 | "a replay appended records" |
| 373 | ); |
| 374 | // The same change under a fresh request and approval is a new proposal. |
| 375 | committed(hook.submit(&sign(&b, unsigned("req-e", "appr-e")), &ws)); |
| 376 | } |
| 377 | |
| 378 | /// #249 A: a caller without the desk key cannot make the hook trust any |
| 379 | /// field. Every variant is refused Unauthenticated before anything runs: |
| 380 | /// zero records, nothing on disk. |
| 381 | #[test] |
| 382 | fn unauthenticated_approval_is_refused_with_no_compose() { |
| 383 | let _home = home_guard::home_slot(); |
| 384 | let _turn = turn(); |
| 385 | let (tmp, b, hook, ws) = setup(); |
| 386 | let good = sign(&b, unsigned("req-a", "appr-a")); |
| 387 | let before = aien_omega_compose::LINKED.then(|| recalled(&b, &[]).records_total); |
| 388 | let mut cases: Vec<(&str, ApprovedProposal)> = Vec::new(); |
| 389 | let mut p = good.clone(); |
| 390 | p.approver = "drake".into(); |
| 391 | cases.push(("forged approver, correct bytes and hashes", p)); |
| 392 | let mut p = good.clone(); |
| 393 | p.request_id = "req-a2".into(); |
| 394 | cases.push(("changed request_id", p)); |
| 395 | let mut p = good.clone(); |
| 396 | p.trace_id = "trace-x".into(); |
| 397 | cases.push(("changed trace_id", p)); |
| 398 | let mut p = good.clone(); |
| 399 | p.approval_id = "appr-a2".into(); |
| 400 | cases.push(("changed approval_id", p)); |
| 401 | let mut p = good.clone(); |
| 402 | p.path = "OTHER.md".into(); |
| 403 | p.approved_proposal_sha256 = interplane_sha(&p.path, &p.content); |
| 404 | cases.push(("changed path, hashes recomputed", p)); |
| 405 | let mut p = good.clone(); |
| 406 | p.content = "other approved bytes\n".into(); |
| 407 | p.content_sha256 = sha(p.content.as_bytes()); |
| 408 | p.approved_proposal_sha256 = interplane_sha(PATH, &p.content); |
| 409 | cases.push(("changed content, hashes recomputed", p)); |
| 410 | let mut p = good.clone(); |
| 411 | p.approval_mac = String::new(); |
| 412 | cases.push(("no MAC", p)); |
| 413 | let mut p = good.clone(); |
| 414 | p.approval_mac = sha(b"caller text is not approval"); |
| 415 | cases.push(("MAC made of caller text", p)); |
| 416 | let other = DeskKey::create(&tmp.path().join("other.key")).unwrap(); |
| 417 | let mut p = good.clone(); |
| 418 | p.approval_mac = other.sign(&p, &ws_of(&b)); |
| 419 | cases.push(("MAC under another key", p)); |
| 420 | let mut p = good.clone(); |
| 421 | p.approval_mac = p.approval_mac.to_uppercase(); |
| 422 | cases.push(("MAC in another hex case", p)); |
| 423 | for (what, p) in &cases { |
| 424 | let e = refused(hook.submit(p, &ws), "Unauthenticated"); |
| 425 | assert_eq!(e.refused_by, "PROPOSAL_REFUSED", "{what}"); |
| 426 | assert_eq!(e.request_id, p.request_id, "{what}: correlation kept"); |
| 427 | } |
| 428 | assert!(!Path::new(&ws).join(PATH).exists()); |
| 429 | if let Some(n) = before { |
| 430 | assert_eq!( |
| 431 | recalled(&b, &[]).records_total, |
| 432 | n, |
| 433 | "a refusal appended records" |
| 434 | ); |
| 435 | // The honest approval was not consumed by the forgeries. |
| 436 | committed(hook.submit(&good, &ws)); |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | /// #249 A key rules: absent, group/other-readable, symlinked or malformed |
| 441 | /// keys are refused NoDesk; refusals and reports never carry the key. |
| 442 | #[test] |
| 443 | fn desk_key_file_rules() { |
| 444 | let _home = home_guard::home_slot(); |
| 445 | let _turn = turn(); |
| 446 | use std::os::unix::fs::PermissionsExt; |
| 447 | let (tmp, b, hook, ws) = setup(); |
| 448 | let path = desk_key_path(b.dir()); |
| 449 | let key_text = std::fs::read_to_string(&path).unwrap(); |
| 450 | let p = sign(&b, unsigned("req-k", "appr-k")); |
| 451 | for mode in [0o640, 0o604, 0o644] { |
| 452 | std::fs::set_permissions(&path, std::fs::Permissions::from_mode(mode)).unwrap(); |
| 453 | let e = refused(hook.submit(&p, &ws), "NoDesk"); |
| 454 | assert!(e.detail.contains("group or other"), "{e}"); |
| 455 | assert!(!e.to_string().contains(key_text.trim())); |
| 456 | } |
| 457 | std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o600)).unwrap(); |
| 458 | // A symlink to a well-formed 0600 key is refused. |
| 459 | let real = tmp.path().join("real.key"); |
| 460 | std::fs::rename(&path, &real).unwrap(); |
| 461 | std::os::unix::fs::symlink(&real, &path).unwrap(); |
| 462 | let e = refused(hook.submit(&p, &ws), "NoDesk"); |
| 463 | assert!(e.detail.contains("symlink"), "{e}"); |
| 464 | std::fs::remove_file(&path).unwrap(); |
| 465 | // Absent. |
| 466 | refused(hook.submit(&p, &ws), "NoDesk"); |
| 467 | // Malformed. |
| 468 | std::fs::write(&path, "not a key\n").unwrap(); |
| 469 | std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o600)).unwrap(); |
| 470 | refused(hook.submit(&p, &ws), "NoDesk"); |
| 471 | // create never overwrites. |
| 472 | assert!(DeskKey::create(&path).is_err()); |
| 473 | std::fs::remove_file(&path).unwrap(); |
| 474 | // Restored key: the MAC verifies again. Rotation: a new key refuses the old MAC. |
| 475 | std::fs::rename(&real, &path).unwrap(); |
| 476 | let k = desk(&b); |
| 477 | assert!(!format!("{k:?}").contains(key_text.trim())); |
| 478 | std::fs::remove_file(&path).unwrap(); |
| 479 | DeskKey::create(&path).unwrap(); |
| 480 | let e = refused(hook.submit(&p, &ws), "Unauthenticated"); |
| 481 | assert!(!e.to_string().contains(key_text.trim())); |
| 482 | if aien_omega_compose::LINKED { |
| 483 | let r = committed(hook.submit(&resign(&b, &p), &ws)); |
| 484 | assert!(!serde_json::to_string(&r).unwrap().contains(key_text.trim())); |
| 485 | assert_ne!(r.desk_key_id, k.id(), "rotation changes the key id"); |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | /// #249 A confinement: a compose home (desk key, journal) inside the |
| 490 | /// workspace, or a workspace inside the compose home, refuses the desk. |
| 491 | #[test] |
| 492 | fn compose_home_overlapping_the_workspace_is_refused() { |
| 493 | let _turn = turn(); |
| 494 | let tmp = tempfile::tempdir().unwrap(); |
| 495 | let ws = tmp.path().join("ws"); |
| 496 | let inner = ws.join(".aien-compose"); |
| 497 | std::fs::create_dir_all(&inner).unwrap(); |
| 498 | let b = Arc::new(ComposeBridge::new(inner.clone(), model_never_called(), "t")); |
| 499 | DeskKey::create(&desk_key_path(&inner)).unwrap(); |
| 500 | let hook = ProposerHook::new(b.clone()); |
| 501 | let p = sign(&b, unsigned("req-c", "appr-c")); |
| 502 | refused(hook.submit(&p, ws.to_str().unwrap()), "Confinement"); |
| 503 | // The other way round: the workspace inside the compose home. |
| 504 | let home = tmp.path().join("home"); |
| 505 | let ws2 = home.join("ws"); |
| 506 | std::fs::create_dir_all(&ws2).unwrap(); |
| 507 | let b2 = Arc::new(ComposeBridge::new(home.clone(), model_never_called(), "t")); |
| 508 | DeskKey::create(&desk_key_path(&home)).unwrap(); |
| 509 | let p2 = sign(&b2, unsigned("req-c", "appr-c")); |
| 510 | refused( |
| 511 | ProposerHook::new(b2).submit(&p2, ws2.to_str().unwrap()), |
| 512 | "Confinement", |
| 513 | ); |
| 514 | // Nothing was ever opened: no journal in either home. |
| 515 | assert!(!inner.join("cortex.cx").exists() && !home.join("cortex.cx").exists()); |
| 516 | } |
| 517 | |
| 518 | /// #249 B, CENTRAL: a fresh bridge over the same compose home (what a |
| 519 | /// daemon restart is) refuses every replay; the retry of the same approval |
| 520 | /// gets the original result back and nothing runs again. |
| 521 | #[test] |
| 522 | #[cfg_attr(not(compose_linked), ignore = "needs librx_compose.a: stub build")] |
| 523 | fn replay_is_refused_after_restart() { |
| 524 | let _home = home_guard::home_slot(); |
| 525 | let _turn = turn(); |
| 526 | let (tmp, b, hook, ws) = setup(); |
| 527 | let p = sign(&b, unsigned("req-r", "appr-r")); |
| 528 | let first = hook.submit(&p, &ws); |
| 529 | if !aien_omega_compose::LINKED { |
| 530 | stub_refused(first); |
| 531 | return; |
| 532 | } |
| 533 | let first = committed(first); |
| 534 | drop(hook); |
| 535 | let dir = b.dir().to_path_buf(); |
| 536 | drop(b); |
| 537 | let b2 = Arc::new(ComposeBridge::new(dir, model_never_called(), "t")); |
| 538 | let hook2 = ProposerHook::new(b2.clone()); |
| 539 | let n = recalled(&b2, &[]).records_total; |
| 540 | let again = hook2.submit(&p, &ws).unwrap(); |
| 541 | assert_eq!(again.state, "ALREADY_COMMITTED"); |
| 542 | assert_eq!(again.compose_proposal_sha256, first.compose_proposal_sha256); |
| 543 | refused( |
| 544 | hook2.submit(&sign(&b2, unsigned("req-r2", "appr-r")), &ws), |
| 545 | "AlreadyCommitted", |
| 546 | ); |
| 547 | refused( |
| 548 | hook2.submit(&sign(&b2, unsigned("req-r", "appr-r2")), &ws), |
| 549 | "AlreadyCommitted", |
| 550 | ); |
| 551 | assert_eq!( |
| 552 | recalled(&b2, &[]).records_total, |
| 553 | n, |
| 554 | "a replay after restart appended records" |
| 555 | ); |
| 556 | let _ = tmp; |
| 557 | } |
| 558 | |
| 559 | /// #249 B: concurrent duplicates. Eight threads submit the same approval and |
| 560 | /// eight more submit new request ids under one approval id: exactly one |
| 561 | /// compose run per approval id. |
| 562 | #[test] |
| 563 | #[cfg_attr(not(compose_linked), ignore = "needs librx_compose.a: stub build")] |
| 564 | fn concurrent_duplicates_run_once() { |
| 565 | let _home = home_guard::home_slot(); |
| 566 | let _turn = turn(); |
| 567 | let (_tmp, b, hook, ws) = setup(); |
| 568 | if !aien_omega_compose::LINKED { |
| 569 | return; |
| 570 | } |
| 571 | let hook = Arc::new(hook); |
| 572 | let same = sign(&b, unsigned("req-cc", "appr-cc")); |
| 573 | let mut handles = Vec::new(); |
| 574 | for i in 0..16 { |
| 575 | let (h, ws) = (hook.clone(), ws.clone()); |
| 576 | let p = if i < 8 { |
| 577 | same.clone() |
| 578 | } else { |
| 579 | sign(&b, unsigned(&format!("req-cc-{i}"), "appr-cc")) |
| 580 | }; |
| 581 | handles.push(std::thread::spawn(move || h.submit(&p, &ws))); |
| 582 | } |
| 583 | let outs: Vec<_> = handles.into_iter().map(|h| h.join().unwrap()).collect(); |
| 584 | let ran: Vec<_> = outs |
| 585 | .iter() |
| 586 | .filter_map(|o| o.as_ref().ok()) |
| 587 | .filter(|r| r.task.is_some()) |
| 588 | .collect(); |
| 589 | assert_eq!(ran.len(), 1, "exactly one submission runs: {outs:?}"); |
| 590 | for o in &outs { |
| 591 | match o { |
| 592 | Ok(r) => assert!(r.state == "COMMITTED" || r.state == "ALREADY_COMMITTED"), |
| 593 | Err(e) => assert!( |
| 594 | ["InFlight", "AlreadyCommitted"].contains(&e.name.as_str()), |
| 595 | "{e}" |
| 596 | ), |
| 597 | } |
| 598 | } |
| 599 | // One promotion in the journal for this approval: one World commit. |
| 600 | let host = recalled(&b, &[]).host; |
| 601 | let claims = host |
| 602 | .iter() |
| 603 | .filter(|r| { |
| 604 | r.text |
| 605 | .as_deref() |
| 606 | .is_some_and(|t| t.contains("\"approved_submission\":\"accepted\"")) |
| 607 | }) |
| 608 | .count(); |
| 609 | assert_eq!(claims, 1); |
| 610 | } |
| 611 | |
| 612 | #[test] |
| 613 | fn approved_hash_is_the_interplane_form() { |
| 614 | let _turn = turn(); |
| 615 | assert_eq!( |
| 616 | approved::approved_proposal_sha256("a/b.md", "x \"q\"\n\u{1}é\n"), |
| 617 | interplane_sha("a/b.md", "x \"q\"\n\u{1}é\n") |
| 618 | ); |
| 619 | } |
| 620 | |
| 621 | /// The path already exists: RunComposeTask would treat it as an edit target |
| 622 | /// and merge the reply into the seed (v7 T5). An approved whole-file |
| 623 | /// proposal must commit byte-exact, with no seed line merged in. |
| 624 | #[test] |
| 625 | #[cfg_attr(not(compose_linked), ignore = "needs librx_compose.a: stub build")] |
| 626 | fn approved_proposal_over_existing_file_commits_byte_exact() { |
| 627 | let _home = home_guard::home_slot(); |
| 628 | let _turn = turn(); |
| 629 | let (_tmp, _b, hook, ws) = setup(); |
| 630 | let seed = "# Notes\nseed line one\nseed line two\n"; |
| 631 | std::fs::write(Path::new(&ws).join(PATH), seed).unwrap(); |
| 632 | // Shares "# Notes" with the seed, so an edit merge would keep both seed lines. |
| 633 | let content = "# Notes\napproved replacement line\n"; |
| 634 | let mut p = unsigned("req-x", "appr-x"); |
| 635 | p.content = content.into(); |
| 636 | p.content_sha256 = sha(content.as_bytes()); |
| 637 | p.approved_proposal_sha256 = interplane_sha(PATH, content); |
| 638 | let p = sign(&_b, p); |
| 639 | let out = hook.submit(&p, &ws); |
| 640 | if !aien_omega_compose::LINKED { |
| 641 | stub_refused(out); |
| 642 | return; |
| 643 | } |
| 644 | let r = committed(out); |
| 645 | let t = r.task.as_ref().unwrap(); |
| 646 | assert!(t.committed, "{t:?}"); |
| 647 | let text = format!("filename: {PATH}\n{content}"); |
| 648 | assert_eq!(t.proposal.as_deref(), Some(text.as_str())); |
| 649 | assert!(!t.proposal.as_deref().unwrap().contains("seed line")); |
| 650 | assert_eq!( |
| 651 | t.proposal_content_sha256.as_deref(), |
| 652 | Some(p.content_sha256.as_str()) |
| 653 | ); |
| 654 | assert_eq!(r.compose_proposal_sha256, sha(text.as_bytes())); |
| 655 | // The hook wrote nothing: the seed is untouched. |
| 656 | assert_eq!( |
| 657 | std::fs::read_to_string(Path::new(&ws).join(PATH)).unwrap(), |
| 658 | seed |
| 659 | ); |
| 660 | } |
| 661 | |
| 662 | /// 476ca4 c17: a valid approval presented with another workspace is refused |
| 663 | /// as Unauthenticated (the workspace is in the MAC), nothing is consumed, and |
| 664 | /// the same approval still commits for the workspace it was made for; the |
| 665 | /// daemon's grant targets the bound workspace. |
| 666 | #[test] |
| 667 | fn approval_redirected_to_another_workspace_is_refused() { |
| 668 | let _home = home_guard::home_slot(); |
| 669 | let _turn = turn(); |
| 670 | let (tmp, b, hook, ws) = setup(); |
| 671 | let other = tmp.path().join("elsewhere"); |
| 672 | std::fs::create_dir_all(&other).unwrap(); |
| 673 | // A symlink to the right workspace resolves to it: same canonical path. |
| 674 | let alias = tmp.path().join("ws-alias"); |
| 675 | std::os::unix::fs::symlink(&ws, &alias).unwrap(); |
| 676 | let p = sign(&b, unsigned("req-w", "appr-w")); |
| 677 | let before = aien_omega_compose::LINKED.then(|| recalled(&b, &[]).records_total); |
| 678 | for w in [other.to_str().unwrap(), "/", tmp.path().to_str().unwrap()] { |
| 679 | let e = hook.submit(&p, w).unwrap_err(); |
| 680 | assert!( |
| 681 | e.name == "Unauthenticated" || e.name == "Confinement", |
| 682 | "{w}: {e}" |
| 683 | ); |
| 684 | } |
| 685 | if let Some(n) = before { |
| 686 | assert_eq!(recalled(&b, &[]).records_total, n, "nothing consumed"); |
| 687 | } |
| 688 | assert!(!other.join(PATH).exists()); |
| 689 | let out = hook.submit(&p, alias.to_str().unwrap()); |
| 690 | if !aien_omega_compose::LINKED { |
| 691 | stub_refused(out); |
| 692 | return; |
| 693 | } |
| 694 | let r = committed(out); |
| 695 | assert_eq!( |
| 696 | r.target.as_deref().map(Path::new), |
| 697 | Some( |
| 698 | canonical_workspace(Path::new(&ws)) |
| 699 | .map(|w| Path::new(&w).join(PATH)) |
| 700 | .unwrap() |
| 701 | .as_path() |
| 702 | ) |
| 703 | ); |
| 704 | } |
| 705 | |
| 706 | /// 476ca4 c25: a journal-level byte copy of the daemon's grant (written past |
| 707 | /// ComposeNote's reserved check, as only direct journal access could) gives |
| 708 | /// one approval two grants: neither opens an intent, nothing is written. |
| 709 | #[test] |
| 710 | #[cfg_attr(not(compose_linked), ignore = "needs librx_compose.a: stub build")] |
| 711 | fn copied_approved_grant_opens_nothing() { |
| 712 | let _home = home_guard::home_slot(); |
| 713 | let _turn = turn(); |
| 714 | let (_tmp, b, hook, ws) = setup(); |
| 715 | let r = committed(hook.submit(&sign(&b, unsigned("req-g", "appr-g")), &ws)); |
| 716 | let a = r.approved_grant.unwrap(); |
| 717 | let g = recalled(&b, &[a]); |
| 718 | let text = g.cited[0].text.clone().unwrap(); |
| 719 | let copy = match b.note_unchecked("authorization", &text, &g.cited[0].links) { |
| 720 | ControlResponse::ComposeNoted(n) => n.id, |
| 721 | other => panic!("bridge-level copy: {other:?}"), |
| 722 | }; |
| 723 | let (pid, start) = effects::self_executor(); |
| 724 | for id in [a, copy] { |
| 725 | let req = IntentRequest { |
| 726 | authorization: id, |
| 727 | proposal_sha256: r.compose_proposal_sha256.clone(), |
| 728 | path: r.path.clone(), |
| 729 | target: r.target.clone().unwrap(), |
| 730 | content_sha256: r.content_sha256.clone(), |
| 731 | executor_pid: pid, |
| 732 | executor_start: start, |
| 733 | }; |
| 734 | match effects::open_intent(&b, &req) { |
| 735 | ControlResponse::Error(e) => { |
| 736 | assert!(e.contains("more than one approved grant"), "#{id}: {e}") |
| 737 | } |
| 738 | other => panic!("#{id} opened: {other:?}"), |
| 739 | } |
| 740 | } |
| 741 | let l = Ledger::from_records(&recalled(&b, &[]).host).unwrap(); |
| 742 | assert!(l.intents.is_empty()); |
| 743 | assert!(!Path::new(&r.target.unwrap()).exists()); |
| 744 | } |
| 745 | |
| 746 | /// A document with several fenced examples and prose after the last one. |
| 747 | const FENCED_DOC: &str = "# FAQ\n\nInstall:\n```bash\ncurl -fsSL https://example.com/i.sh | sh\n```\nThen verify:\n```bash\ntool --version\n```\nLast words after the final example.\n"; |
| 748 | |
| 749 | /// One approved document with embedded fences, through the real approval and |
| 750 | /// effect path: the approval binds sha256(content); the proposal text reads |
| 751 | /// back byte for byte; the intent only opens for that digest; and the effect |
| 752 | /// is DONE only when the bytes on disk hash to the approved digest. A write cut |
| 753 | /// at the first inner fence (the D5 truncation) is UNRESOLVED, never DONE. |
| 754 | #[test] |
| 755 | #[cfg_attr(not(compose_linked), ignore = "needs librx_compose.a: stub build")] |
| 756 | fn approved_document_with_fences_is_saved_byte_exact_or_not_done() { |
| 757 | let _home = home_guard::home_slot(); |
| 758 | let _turn = turn(); |
| 759 | let (_tmp, b, hook, ws) = setup(); |
| 760 | // The compose template keeps every byte (check_file_proposal reads it back). |
| 761 | let text = format!("filename: {PATH}\n{FENCED_DOC}"); |
| 762 | let back = aien_runtime::check_file_proposal(&text).unwrap(); |
| 763 | assert_eq!(back.content, FENCED_DOC); |
| 764 | let mut p = unsigned("req-f1", "appr-f1"); |
| 765 | p.content = FENCED_DOC.into(); |
| 766 | p.content_sha256 = sha(FENCED_DOC.as_bytes()); |
| 767 | p.approved_proposal_sha256 = interplane_sha(PATH, FENCED_DOC); |
| 768 | let p = sign(&b, p); |
| 769 | let out = hook.submit(&p, &ws); |
| 770 | if !aien_omega_compose::LINKED { |
| 771 | stub_refused(out); |
| 772 | return; |
| 773 | } |
| 774 | let r = committed(out); |
| 775 | assert_eq!(r.content_sha256, sha(FENCED_DOC.as_bytes())); |
| 776 | assert_eq!(r.compose_proposal_sha256, sha(text.as_bytes())); |
| 777 | let a = r.approved_grant.expect("grant"); |
| 778 | let tgt = r.target.clone().unwrap(); |
| 779 | let (pid, start) = effects::self_executor(); |
| 780 | let req = |content_sha: &str| IntentRequest { |
| 781 | authorization: a, |
| 782 | proposal_sha256: r.compose_proposal_sha256.clone(), |
| 783 | path: r.path.clone(), |
| 784 | target: tgt.clone(), |
| 785 | content_sha256: content_sha.to_string(), |
| 786 | executor_pid: pid, |
| 787 | executor_start: start, |
| 788 | }; |
| 789 | // An intent for any other bytes (for example the truncated text) is refused. |
| 790 | let cut = FENCED_DOC.split("```bash").next().unwrap(); |
| 791 | match effects::open_intent(&b, &req(&sha(cut.as_bytes()))) { |
| 792 | ControlResponse::Error(e) => assert!(e.contains("NotAuthorized"), "{e}"), |
| 793 | other => panic!("want refusal, got {other:?}"), |
| 794 | } |
| 795 | let i = match effects::open_intent(&b, &req(&r.content_sha256)) { |
| 796 | ControlResponse::ComposeNoted(n) => n.id, |
| 797 | other => panic!("intent: {other:?}"), |
| 798 | }; |
| 799 | let state_of = |k: u64| -> Value { |
| 800 | let rec = recalled(&b, &[k]); |
| 801 | serde_json::from_str(rec.cited[0].text.as_deref().unwrap()).unwrap() |
| 802 | }; |
| 803 | // Truncated bytes on disk: the ack records UNRESOLVED, not DONE. |
| 804 | let target = Path::new(&tgt).to_path_buf(); |
| 805 | std::fs::write(&target, cut).unwrap(); |
| 806 | let k = match effects::ack(&b, i, &json!({})) { |
| 807 | ControlResponse::ComposeNoted(n) => n.id, |
| 808 | other => panic!("ack: {other:?}"), |
| 809 | }; |
| 810 | let v = state_of(k); |
| 811 | assert_eq!(v["state"], "UNRESOLVED", "{v}"); |
| 812 | assert_ne!(sha(&std::fs::read(&target).unwrap()), p.content_sha256); |
| 813 | |
| 814 | // Second approval, same document at another path: exact bytes are DONE. |
| 815 | let mut p2 = unsigned("req-f2", "appr-f2"); |
| 816 | p2.path = "FAQ.md".into(); |
| 817 | p2.content = FENCED_DOC.into(); |
| 818 | p2.content_sha256 = sha(FENCED_DOC.as_bytes()); |
| 819 | p2.approved_proposal_sha256 = interplane_sha("FAQ.md", FENCED_DOC); |
| 820 | let r2 = committed(hook.submit(&sign(&b, p2.clone()), &ws)); |
| 821 | let a2 = r2.approved_grant.expect("grant"); |
| 822 | let tgt2 = r2.target.clone().unwrap(); |
| 823 | let i2 = match effects::open_intent( |
| 824 | &b, |
| 825 | &IntentRequest { |
| 826 | authorization: a2, |
| 827 | proposal_sha256: r2.compose_proposal_sha256.clone(), |
| 828 | path: r2.path.clone(), |
| 829 | target: tgt2.clone(), |
| 830 | content_sha256: r2.content_sha256.clone(), |
| 831 | executor_pid: pid, |
| 832 | executor_start: start, |
| 833 | }, |
| 834 | ) { |
| 835 | ControlResponse::ComposeNoted(n) => n.id, |
| 836 | other => panic!("intent: {other:?}"), |
| 837 | }; |
| 838 | std::fs::write(&tgt2, FENCED_DOC).unwrap(); |
| 839 | let k2 = match effects::ack(&b, i2, &json!({})) { |
| 840 | ControlResponse::ComposeNoted(n) => n.id, |
| 841 | other => panic!("ack: {other:?}"), |
| 842 | }; |
| 843 | assert_eq!(state_of(k2)["state"], "DONE"); |
| 844 | let disk = std::fs::read(&tgt2).unwrap(); |
| 845 | assert_eq!(disk, FENCED_DOC.as_bytes()); |
| 846 | assert_eq!(sha(&disk), p2.content_sha256); |
| 847 | } |
| 848 | |
| 849 | // ---- goal requirements bound into the desk-signed approval ---- |
| 850 | |
| 851 | /// `unsigned` with a requirement goal, sealed by the desk. |
| 852 | fn with_goal( |
| 853 | b: &ComposeBridge, |
| 854 | request: &str, |
| 855 | approval: &str, |
| 856 | goal: Option<&str>, |
| 857 | ) -> ApprovedProposal { |
| 858 | let mut p = unsigned(request, approval); |
| 859 | p.requirements = goal.map(str::to_string); |
| 860 | sign(b, p) |
| 861 | } |
| 862 | |
| 863 | #[test] |
| 864 | fn bound_requirements_the_bytes_miss_are_refused_and_nothing_is_consumed() { |
| 865 | let _home = home_guard::home_slot(); |
| 866 | let _turn = turn(); |
| 867 | let (_tmp, b, hook, ws) = setup(); |
| 868 | let p = with_goal( |
| 869 | &b, |
| 870 | "req-rq1", |
| 871 | "appr-rq1", |
| 872 | Some("write NOTES.md in at least 5 lines"), |
| 873 | ); |
| 874 | let e = refused(hook.submit(&p, &ws), "RequirementsUnmet"); |
| 875 | assert!( |
| 876 | e.detail.contains("at least 5 non-empty lines, found 1"), |
| 877 | "{e}" |
| 878 | ); |
| 879 | // Refused before the claim: no replay claim was consumed, nothing was |
| 880 | // written, and the same ids still work for bytes that meet the goal. |
| 881 | assert!(!Path::new(&ws).join(PATH).exists()); |
| 882 | if !aien_omega_compose::LINKED { |
| 883 | return; |
| 884 | } |
| 885 | let ok = with_goal( |
| 886 | &b, |
| 887 | "req-rq1", |
| 888 | "appr-rq1", |
| 889 | Some("write NOTES.md in at least 1 line."), |
| 890 | ); |
| 891 | let r = committed(hook.submit(&ok, &ws)); |
| 892 | assert_eq!(r.content_sha256, sha(CONTENT.as_bytes())); |
| 893 | assert_eq!( |
| 894 | r.task.as_ref().unwrap().proposal_content_sha256.as_deref(), |
| 895 | Some(r.content_sha256.as_str()) |
| 896 | ); |
| 897 | } |
| 898 | |
| 899 | #[test] |
| 900 | fn bytes_meeting_the_bound_requirements_commit_with_equal_sha256() { |
| 901 | let _home = home_guard::home_slot(); |
| 902 | let _turn = turn(); |
| 903 | let (_tmp, b, hook, ws) = setup(); |
| 904 | let p = with_goal( |
| 905 | &b, |
| 906 | "req-rq2", |
| 907 | "appr-rq2", |
| 908 | Some("write it in at least 1 line"), |
| 909 | ); |
| 910 | let out = hook.submit(&p, &ws); |
| 911 | if !aien_omega_compose::LINKED { |
| 912 | stub_refused(out); |
| 913 | return; |
| 914 | } |
| 915 | let r = committed(out); |
| 916 | let t = r.task.as_ref().unwrap(); |
| 917 | assert!(t.committed); |
| 918 | assert_eq!(r.content_sha256, sha(CONTENT.as_bytes())); |
| 919 | assert_eq!( |
| 920 | t.proposal_content_sha256.as_deref(), |
| 921 | Some(r.content_sha256.as_str()) |
| 922 | ); |
| 923 | } |
| 924 | |
| 925 | #[test] |
| 926 | fn explicitly_signed_empty_requirements_are_allowed() { |
| 927 | let _home = home_guard::home_slot(); |
| 928 | let _turn = turn(); |
| 929 | let (_tmp, b, hook, ws) = setup(); |
| 930 | let p = with_goal(&b, "req-rq3", "appr-rq3", Some("")); |
| 931 | let out = hook.submit(&p, &ws); |
| 932 | if !aien_omega_compose::LINKED { |
| 933 | stub_refused(out); |
| 934 | return; |
| 935 | } |
| 936 | committed(out); |
| 937 | } |
| 938 | |
| 939 | #[test] |
| 940 | fn missing_requirement_binding_is_refused_even_when_signed() { |
| 941 | let _turn = turn(); |
| 942 | let (_tmp, b, hook, ws) = setup(); |
| 943 | let p = with_goal(&b, "req-rq4", "appr-rq4", None); |
| 944 | // Refused in the checks that need no compose home, stub build included. |
| 945 | refused(hook.submit(&p, &ws), "RequirementsUnbound"); |
| 946 | } |
| 947 | |
| 948 | #[test] |
| 949 | fn tampered_requirement_set_fails_the_mac() { |
| 950 | let _turn = turn(); |
| 951 | let (_tmp, b, hook, ws) = setup(); |
| 952 | // The desk signed a demanding goal; the caller swaps in an empty one. |
| 953 | let mut p = with_goal( |
| 954 | &b, |
| 955 | "req-rq5", |
| 956 | "appr-rq5", |
| 957 | Some("write NOTES.md in at least 5 lines"), |
| 958 | ); |
| 959 | p.requirements = Some(String::new()); |
| 960 | refused(hook.submit(&p, &ws), "Unauthenticated"); |
| 961 | // And a bound goal cannot be dropped to None either. |
| 962 | let mut q = with_goal(&b, "req-rq6", "appr-rq6", Some("")); |
| 963 | q.requirements = None; |
| 964 | refused(hook.submit(&q, &ws), "RequirementsUnbound"); |
| 965 | } |
| 966 | |
| 967 | #[test] |
| 968 | fn uncertain_bound_requirements_are_refused() { |
| 969 | let _turn = turn(); |
| 970 | let (_tmp, b, hook, ws) = setup(); |
| 971 | let p = with_goal( |
| 972 | &b, |
| 973 | "req-rq7", |
| 974 | "appr-rq7", |
| 975 | Some("at least 3 lines of context"), |
| 976 | ); |
| 977 | refused(hook.submit(&p, &ws), "RequirementsUncertain"); |
| 978 | } |
| 979 | |
| 980 | #[test] |
| 981 | fn bound_added_line_counts_are_measured_against_the_existing_file() { |
| 982 | let _home = home_guard::home_slot(); |
| 983 | let _turn = turn(); |
| 984 | let (_tmp, b, hook, ws) = setup(); |
| 985 | std::fs::write(Path::new(&ws).join(PATH), "one\ntwo\n").unwrap(); |
| 986 | // The approved bytes add one line to the two that exist. |
| 987 | let mut p = unsigned("req-al1", "appr-al1"); |
| 988 | p.content = "one\ntwo\nthree\n".into(); |
| 989 | p.approved_proposal_sha256 = interplane_sha(PATH, &p.content); |
| 990 | p.content_sha256 = sha(p.content.as_bytes()); |
| 991 | p.requirements = Some("Add 2 lines to NOTES.md".into()); |
| 992 | p.requirements_base = Some(sha(b"one\ntwo\n")); |
| 993 | let two = sign(&b, p.clone()); |
| 994 | let e = refused(hook.submit(&two, &ws), "RequirementsUnmet"); |
| 995 | assert!( |
| 996 | e.detail |
| 997 | .contains("exactly 2 added non-empty lines, found 1"), |
| 998 | "{e}" |
| 999 | ); |
| 1000 | // One added line meets "Add one line": never a requirement refusal. |
| 1001 | p.request_id = "req-al2".into(); |
| 1002 | p.trace_id = "trace-req-al2".into(); |
| 1003 | p.approval_id = "appr-al2".into(); |
| 1004 | p.requirements = Some("Add one line to NOTES.md".into()); |
| 1005 | let one = sign(&b, p); |
| 1006 | if let Err(e) = hook.submit(&one, &ws) { |
| 1007 | assert!(!e.name.starts_with("Requirements"), "{e}"); |
| 1008 | } |
| 1009 | } |
| 1010 | |
| 1011 | #[test] |
| 1012 | fn a_path_that_reads_like_a_requirement_is_not_one() { |
| 1013 | let _home = home_guard::home_slot(); |
| 1014 | let _turn = turn(); |
| 1015 | let (_tmp, b, hook, ws) = setup(); |
| 1016 | let path = "docs/add-2-lines-to-10-lines.md"; |
| 1017 | std::fs::create_dir_all(Path::new(&ws).join("docs")).unwrap(); |
| 1018 | let mut p = unsigned("req-mg1", "appr-mg1"); |
| 1019 | p.path = path.into(); |
| 1020 | p.approved_proposal_sha256 = interplane_sha(path, CONTENT); |
| 1021 | let p = sign(&b, p); |
| 1022 | let out = hook.submit(&p, &ws); |
| 1023 | if !aien_omega_compose::LINKED { |
| 1024 | stub_refused(out); |
| 1025 | return; |
| 1026 | } |
| 1027 | committed(out); |
| 1028 | } |
| 1029 | |
| 1030 | #[test] |
| 1031 | fn added_line_goal_without_a_bound_base_or_with_a_stale_one_is_refused() { |
| 1032 | let _turn = turn(); |
| 1033 | let (_tmp, b, hook, ws) = setup(); |
| 1034 | std::fs::write(Path::new(&ws).join(PATH), "one\ntwo\n").unwrap(); |
| 1035 | let mut p = unsigned("req-bs1", "appr-bs1"); |
| 1036 | p.content = "one\ntwo\nthree\n".into(); |
| 1037 | p.approved_proposal_sha256 = interplane_sha(PATH, &p.content); |
| 1038 | p.content_sha256 = sha(p.content.as_bytes()); |
| 1039 | p.requirements = Some("Add one line to NOTES.md".into()); |
| 1040 | // No base bound: the check could not be tied to a file state. |
| 1041 | let none = sign(&b, p.clone()); |
| 1042 | refused(hook.submit(&none, &ws), "RequirementsUnbound"); |
| 1043 | // A base that is not the file now (it changed after the desk looked). |
| 1044 | p.request_id = "req-bs2".into(); |
| 1045 | p.trace_id = "trace-req-bs2".into(); |
| 1046 | p.approval_id = "appr-bs2".into(); |
| 1047 | p.requirements_base = Some(sha(b"something else\n")); |
| 1048 | let stale = sign(&b, p.clone()); |
| 1049 | let e = refused(hook.submit(&stale, &ws), "BaseChanged"); |
| 1050 | assert!(e.detail.contains("changed since the desk bound it"), "{e}"); |
| 1051 | // The base is covered by the MAC: swapping it after signing fails authentication. |
| 1052 | p.request_id = "req-bs3".into(); |
| 1053 | p.trace_id = "trace-req-bs3".into(); |
| 1054 | p.approval_id = "appr-bs3".into(); |
| 1055 | p.requirements_base = Some(sha(b"one\ntwo\n")); |
| 1056 | let mut signed = sign(&b, p); |
| 1057 | signed.requirements_base = Some(sha(b"something else\n")); |
| 1058 | refused(hook.submit(&signed, &ws), "Unauthenticated"); |
| 1059 | } |