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 / objective_identity_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 #408 (RECOVERY-AUDIT-2026-10-11 row 4): the objective id is |
| 17 | //! carried by the grant, the intent, the ack and the reconcile records, and |
| 18 | //! the objective record is re-read after a restart. |
| 19 | //! |
| 20 | //! The real compose home through the bridge, as the daemon drives it (no |
| 21 | //! socket). A restart is a new bridge on the same home. The objective record |
| 22 | //! is written here exactly as `aien-cli objective record` writes it (sc#388). |
| 23 | //! In a stub build only the refusal of the compose run is checked. |
| 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::{ |
| 30 | ComposeNoteReport, ComposeReconcileReport, ControlResponse, ReconcileOutcome, |
| 31 | }; |
| 32 | use aien_runtime::effects::{self, IntentRequest, Ledger, MintRequest}; |
| 33 | use aien_runtime::objective; |
| 34 | use serde_json::{json, Value}; |
| 35 | use sha2::{Digest, Sha256}; |
| 36 | use std::path::{Path, PathBuf}; |
| 37 | use std::sync::Arc; |
| 38 | |
| 39 | const PROPOSAL: &str = "filename: NOTES.md\nkeep every change inside the workspace\n"; |
| 40 | const CONTENT: &str = "keep every change inside the workspace\n"; |
| 41 | const GOAL: &str = "write the note"; |
| 42 | |
| 43 | fn proposer() -> ComposeProposer { |
| 44 | Arc::new(|_prompt: &str, _limit: std::time::Duration| { |
| 45 | Ok(Generation { |
| 46 | text: PROPOSAL.to_string(), |
| 47 | tokens: 8, |
| 48 | finish_reason: Some("eos".into()), |
| 49 | ..Default::default() |
| 50 | }) |
| 51 | }) |
| 52 | } |
| 53 | |
| 54 | fn sha(b: &[u8]) -> String { |
| 55 | hex(&Sha256::digest(b)) |
| 56 | } |
| 57 | |
| 58 | fn noted(r: ControlResponse) -> ComposeNoteReport { |
| 59 | match r { |
| 60 | ControlResponse::ComposeNoted(n) => n, |
| 61 | other => panic!("expected ComposeNoted, got {other:?}"), |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | fn refused(r: ControlResponse, name: &str) { |
| 66 | match r { |
| 67 | ControlResponse::Error(e) => assert!( |
| 68 | e.starts_with(&format!("EFFECT_REFUSED {name}:")), |
| 69 | "want {name}, got {e}" |
| 70 | ), |
| 71 | other => panic!("want EFFECT_REFUSED {name}, got {other:?}"), |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | fn reconciled(r: ControlResponse) -> ComposeReconcileReport { |
| 76 | match r { |
| 77 | ControlResponse::ComposeReconciled(r) => *r, |
| 78 | other => panic!("expected ComposeReconciled, got {other:?}"), |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | fn ledger(b: &ComposeBridge) -> Ledger { |
| 83 | match b.recall(&[], None) { |
| 84 | ControlResponse::ComposeRecalled(r) => Ledger::from_records(&r.host).unwrap(), |
| 85 | other => panic!("recall: {other:?}"), |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | fn text_of(b: &ComposeBridge, record: u64) -> Value { |
| 90 | match b.recall(&[record], None) { |
| 91 | ControlResponse::ComposeRecalled(r) => { |
| 92 | serde_json::from_str(r.cited[0].text.as_deref().unwrap()).unwrap() |
| 93 | } |
| 94 | other => panic!("recall: {other:?}"), |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | /// The record `aien-cli objective record --text` writes (sc#388). |
| 99 | fn write_record(prov: &Path, text: &str) -> (String, PathBuf) { |
| 100 | let id = objective::objective_id(text); |
| 101 | let d = prov.join("objectives"); |
| 102 | std::fs::create_dir_all(&d).unwrap(); |
| 103 | let p = d.join(format!("{id}.json")); |
| 104 | let rec = json!({"version": 1, "objective_id": id, "objective_text": text, |
| 105 | "objective_text_sha256": sha(text.as_bytes()), "effect_class": "WORKSPACE_WRITE", |
| 106 | "recorded_before_work": true, "operator_surface": "aien-cli", |
| 107 | "utc": "2026-10-11T00:00:00Z"}); |
| 108 | std::fs::write(&p, serde_json::to_vec_pretty(&rec).unwrap()).unwrap(); |
| 109 | (id, p) |
| 110 | } |
| 111 | |
| 112 | /// Rewrite the record in place with other text and a consistent digest: what |
| 113 | /// an edit of the file by hand would leave. |
| 114 | fn alter_record(p: &Path) { |
| 115 | let mut v: Value = serde_json::from_slice(&std::fs::read(p).unwrap()).unwrap(); |
| 116 | let t = "write the note, then delete the inbox"; |
| 117 | v["objective_text"] = json!(t); |
| 118 | v["objective_text_sha256"] = json!(sha(t.as_bytes())); |
| 119 | std::fs::write(p, serde_json::to_vec_pretty(&v).unwrap()).unwrap(); |
| 120 | } |
| 121 | |
| 122 | struct Fx { |
| 123 | ws: PathBuf, |
| 124 | target: String, |
| 125 | promotion: u64, |
| 126 | } |
| 127 | |
| 128 | impl Fx { |
| 129 | fn compose(b: &ComposeBridge, ws: &Path) -> u64 { |
| 130 | match b.run_task(GOAL, ws.to_str().unwrap()) { |
| 131 | ControlResponse::ComposeTaskResult(r) => { |
| 132 | assert!(r.committed, "{r:?}"); |
| 133 | r.cx_promotion |
| 134 | } |
| 135 | other => panic!("compose: {other:?}"), |
| 136 | } |
| 137 | } |
| 138 | fn mint(&self, b: &ComposeBridge, objective_id: Option<&str>) -> ControlResponse { |
| 139 | effects::authorize_with_objective( |
| 140 | b, |
| 141 | &MintRequest { |
| 142 | cx_promotion: self.promotion, |
| 143 | proposal_sha256: sha(PROPOSAL.as_bytes()), |
| 144 | workspace: self.ws.display().to_string(), |
| 145 | approver: "drake".into(), |
| 146 | constraints: vec![], |
| 147 | }, |
| 148 | None, |
| 149 | objective_id, |
| 150 | ) |
| 151 | } |
| 152 | fn req(&self, a: u64, alive: bool) -> IntentRequest { |
| 153 | let (pid, start) = if alive { |
| 154 | effects::self_executor() |
| 155 | } else { |
| 156 | (0, 0) |
| 157 | }; |
| 158 | IntentRequest { |
| 159 | authorization: a, |
| 160 | proposal_sha256: sha(PROPOSAL.as_bytes()), |
| 161 | path: "NOTES.md".into(), |
| 162 | target: self.target.clone(), |
| 163 | content_sha256: sha(CONTENT.as_bytes()), |
| 164 | executor_pid: pid, |
| 165 | executor_start: start, |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | fn bridge(home: &Path) -> ComposeBridge { |
| 171 | ComposeBridge::new(home.to_path_buf(), proposer(), "test:fixed-proposer") |
| 172 | .with_authorize_requires_desk(false) |
| 173 | } |
| 174 | |
| 175 | fn outcome_for(line: &str, intent: u64) -> ReconcileOutcome { |
| 176 | let json = &line[line.find("; [").expect("outcomes in the line") + 2..]; |
| 177 | let all: Vec<ReconcileOutcome> = serde_json::from_str(json).unwrap(); |
| 178 | all.into_iter().find(|o| o.intent == intent).unwrap() |
| 179 | } |
| 180 | |
| 181 | #[test] |
| 182 | fn objective_identity_survives_a_restart_and_is_re_read() { |
| 183 | let _home = home_guard::home_slot(); |
| 184 | let tmp = tempfile::tempdir().unwrap(); |
| 185 | let prov = tmp.path().join("prov"); |
| 186 | // The daemon and the CLI resolve the record directory the same way. |
| 187 | std::env::set_var("AIEN_PROVENANCE_DIR", &prov); |
| 188 | let home = tmp.path().join("compose"); |
| 189 | let ws = std::fs::canonicalize({ |
| 190 | let w = tmp.path().join("ws"); |
| 191 | std::fs::create_dir_all(&w).unwrap(); |
| 192 | w |
| 193 | }) |
| 194 | .unwrap(); |
| 195 | let b = bridge(&home); |
| 196 | if !aien_omega_compose::LINKED { |
| 197 | assert!(matches!( |
| 198 | b.run_task(GOAL, ws.to_str().unwrap()), |
| 199 | ControlResponse::Error(_) |
| 200 | )); |
| 201 | return; |
| 202 | } |
| 203 | let mut f = Fx { |
| 204 | target: ws.join("NOTES.md").display().to_string(), |
| 205 | promotion: Fx::compose(&b, &ws), |
| 206 | ws: ws.clone(), |
| 207 | }; |
| 208 | // The daemon's own commit record names the goal it composed for. |
| 209 | let l = ledger(&b); |
| 210 | let commit = l.commits.values().last().unwrap(); |
| 211 | assert_eq!( |
| 212 | commit.goal_sha256.as_deref(), |
| 213 | Some(sha(GOAL.as_bytes()).as_str()) |
| 214 | ); |
| 215 | |
| 216 | // 1. Refusals by name; nothing is minted. |
| 217 | let missing = objective::objective_id("never recorded"); |
| 218 | refused(f.mint(&b, Some(&missing)), "ObjectiveMissing"); |
| 219 | let (other, _) = write_record(&prov, "some other objective"); |
| 220 | refused(f.mint(&b, Some(&other)), "ObjectiveMismatch"); |
| 221 | refused(f.mint(&b, Some("not-an-id")), "ObjectiveMissing"); |
| 222 | assert!( |
| 223 | ledger(&b).grants.is_empty(), |
| 224 | "a refused authorize mints nothing" |
| 225 | ); |
| 226 | |
| 227 | // 2. Bound grant, intent from an executor that dies: the id rides along. |
| 228 | let (oid, rec_path) = write_record(&prov, GOAL); |
| 229 | let rec_sha = sha(&std::fs::read(&rec_path).unwrap()); |
| 230 | let g = noted(f.mint(&b, Some(&oid))).id; |
| 231 | let gt = text_of(&b, g); |
| 232 | assert_eq!(gt["objective_id"], json!(oid)); |
| 233 | assert_eq!(gt["objective_text_sha256"], json!(sha(GOAL.as_bytes()))); |
| 234 | assert_eq!(gt["objective_record_sha256"], json!(rec_sha)); |
| 235 | let i = noted(effects::open_intent(&b, &f.req(g, false))).id; |
| 236 | assert_eq!(text_of(&b, i)["objective_id"], json!(oid)); |
| 237 | assert_eq!(ledger(&b).view()["intents"][0]["objective_id"], json!(oid)); |
| 238 | |
| 239 | // 3. Restart with the record intact: reconcile binds the intent to it. |
| 240 | drop(b); |
| 241 | let b = bridge(&home); |
| 242 | let line = effects::reconcile_at_start(&b); |
| 243 | let o = outcome_for(&line, i); |
| 244 | assert_eq!(o.state, "NOT_DONE", "{line}"); |
| 245 | let ob = o.objective.expect("binding reported"); |
| 246 | assert_eq!( |
| 247 | (ob.objective_id.as_str(), ob.record.as_str()), |
| 248 | (oid.as_str(), "bound") |
| 249 | ); |
| 250 | assert_eq!(ob.record_unchanged, Some(true)); |
| 251 | assert_eq!(text_of(&b, o.record.unwrap())["objective_id"], json!(oid)); |
| 252 | refused(effects::open_intent(&b, &f.req(g, true)), "AlreadySpent"); |
| 253 | |
| 254 | // 4. Record removed: a new approval is refused by name; restored: minted. |
| 255 | std::fs::rename(&rec_path, tmp.path().join("moved.json")).unwrap(); |
| 256 | refused(f.mint(&b, Some(&oid)), "ObjectiveMissing"); |
| 257 | std::fs::rename(tmp.path().join("moved.json"), &rec_path).unwrap(); |
| 258 | let g2 = noted(f.mint(&b, Some(&oid))).id; |
| 259 | |
| 260 | // 5. Record altered after the grant: the intent is refused, nothing written. |
| 261 | let saved = std::fs::read(&rec_path).unwrap(); |
| 262 | alter_record(&rec_path); |
| 263 | refused( |
| 264 | effects::open_intent(&b, &f.req(g2, true)), |
| 265 | "ObjectiveMismatch", |
| 266 | ); |
| 267 | assert!( |
| 268 | !ledger(&b).spent.contains_key(&g2), |
| 269 | "no intent on a refusal" |
| 270 | ); |
| 271 | std::fs::write(&rec_path, &saved).unwrap(); |
| 272 | |
| 273 | // 6. Restored: the effect runs once and the ack names the objective. |
| 274 | let i2 = noted(effects::open_intent(&b, &f.req(g2, true))).id; |
| 275 | std::fs::write(&f.target, CONTENT).unwrap(); |
| 276 | let k = noted(effects::ack(&b, i2, &json!({}))); |
| 277 | let kt = text_of(&b, k.id); |
| 278 | assert_eq!( |
| 279 | (kt["state"].clone(), kt["objective_id"].clone()), |
| 280 | (json!("DONE"), json!(oid)) |
| 281 | ); |
| 282 | |
| 283 | // 7. Restart with an open bound intent whose record is gone: the intent is |
| 284 | // still settled by the world, and the missing record is named, not silent. |
| 285 | std::fs::remove_file(&f.target).unwrap(); |
| 286 | f.promotion = Fx::compose(&b, &ws); |
| 287 | let g3 = noted(f.mint(&b, Some(&oid))).id; |
| 288 | let i3 = noted(effects::open_intent(&b, &f.req(g3, false))).id; |
| 289 | drop(b); |
| 290 | std::fs::remove_file(&rec_path).unwrap(); |
| 291 | let b = bridge(&home); |
| 292 | let line = effects::reconcile_at_start(&b); |
| 293 | let o = outcome_for(&line, i3); |
| 294 | assert_eq!(o.state, "NOT_DONE", "{line}"); |
| 295 | let ob = o.objective.expect("binding reported"); |
| 296 | assert_eq!(ob.record, "ObjectiveMissing", "{line}"); |
| 297 | assert!(ob.detail.is_some()); |
| 298 | refused(f.mint(&b, Some(&oid)), "ObjectiveMissing"); |
| 299 | |
| 300 | // 8. Legacy path: a grant without an objective is minted and reconciled |
| 301 | // exactly as before; nothing about objectives appears. |
| 302 | f.promotion = Fx::compose(&b, &ws); |
| 303 | let g4 = noted(f.mint(&b, None)).id; |
| 304 | assert!(text_of(&b, g4).get("objective_id").is_none()); |
| 305 | let i4 = noted(effects::open_intent(&b, &f.req(g4, false))).id; |
| 306 | assert!(text_of(&b, i4).get("objective_id").is_none()); |
| 307 | let r = reconciled(effects::reconcile(&b, None, "test")); |
| 308 | let o = r.outcomes.iter().find(|o| o.intent == i4).unwrap(); |
| 309 | assert!(o.objective.is_none()); |
| 310 | assert!(!serde_json::to_string(o).unwrap().contains("objective")); |
| 311 | std::env::remove_var("AIEN_PROVENANCE_DIR"); |
| 312 | } |