interplane.Get Omega
Read in your language

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

branch main Branches Tags History

aien-sovereign-core-mv / crates / aien-runtime / tests / confine_race_test.rs

205 lines · 7673 bytes Raw Blame History
1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2026 Drake Stapleton (AIEN / aien-dev)
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16//! sovereign-core #267 follow-up: the REAL `mint_grant` (authorize) and
17//! `open_intent` calls, with a swap fired INSIDE the call between the
18//! confinement check and the read. Each asserts an exact outcome: the recorded
19//! prior is the hash of the file the confinement check saw, or a specific
20//! refusal. Needs the linked composition archive; IGNORED in a stub build.
21//! branch must give the identical kind for every row.
22//!
23//! Needs the linked composition archive; IGNORED in a stub build.
24#[path = "support/home_guard.rs"]
25mod home_guard;
26
27use aien_omega_compose::hex;
28use aien_runtime::compose::{ComposeBridge, ComposeProposer, Generation};
29use aien_runtime::control::{ComposeNoteReport, ComposeTaskReport, ControlResponse};
30use aien_runtime::effects::test_hooks;
31use aien_runtime::effects::{self, IntentRequest, MintRequest};
32use sha2::{Digest, Sha256};
33use std::path::PathBuf;
34use std::sync::Arc;
35
36const PROPOSAL: &str = "filename: d/NOTES.md\nkeep every change inside the workspace\n";
37const CONTENT: &str = "keep every change inside the workspace\n";
38
39fn proposer() -> ComposeProposer {
40 Arc::new(|_p: &str, _l: 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
50fn sha(b: &[u8]) -> String {
51 hex(&Sha256::digest(b))
52}
53
54static ONE_HOME: std::sync::Mutex<()> = std::sync::Mutex::new(());
55
56struct Fx {
57 _turn: std::sync::MutexGuard<'static, ()>,
58 _tmp: tempfile::TempDir,
59 root: PathBuf,
60 ws: PathBuf,
61 b: ComposeBridge,
62 report: ComposeTaskReport,
63}
64
65impl Fx {
66 fn new() -> Option<Fx> {
67 let turn = ONE_HOME.lock().unwrap_or_else(|e| e.into_inner());
68 let tmp = tempfile::tempdir().unwrap();
69 let root = std::fs::canonicalize(tmp.path()).unwrap();
70 let ws = root.join("ws");
71 std::fs::create_dir_all(ws.join("d")).unwrap();
72 let b = ComposeBridge::new(root.join("compose"), proposer(), "test:fixed-proposer")
73 .with_authorize_requires_desk(false);
74 let run = b.run_task("write the note", ws.to_str().unwrap());
75 if !aien_omega_compose::LINKED {
76 return None;
77 }
78 let ControlResponse::ComposeTaskResult(report) = run else {
79 panic!("run: {run:?}")
80 };
81 assert!(report.committed, "{report:?}");
82 Some(Fx {
83 _turn: turn,
84 _tmp: tmp,
85 root,
86 ws,
87 b,
88 report: *report,
89 })
90 }
91 fn file(&self) -> PathBuf {
92 self.ws.join("d/NOTES.md")
93 }
94 fn mint_req(&self) -> MintRequest {
95 MintRequest {
96 cx_promotion: self.report.cx_promotion,
97 proposal_sha256: sha(PROPOSAL.as_bytes()),
98 workspace: self.ws.display().to_string(),
99 approver: "drake".into(),
100 constraints: vec![],
101 }
102 }
103 fn req(&self, a: u64, wrong_grant: bool) -> IntentRequest {
104 let (pid, start) = effects::self_executor();
105 IntentRequest {
106 authorization: a,
107 proposal_sha256: sha(PROPOSAL.as_bytes()),
108 path: "d/NOTES.md".into(),
109 target: self.file().display().to_string(),
110 content_sha256: if wrong_grant {
111 sha(b"other")
112 } else {
113 sha(CONTENT.as_bytes())
114 },
115 executor_pid: pid,
116 executor_start: start,
117 }
118 }
119}
120
121fn id(r: ControlResponse) -> u64 {
122 match r {
123 ControlResponse::ComposeNoted(ComposeNoteReport { id, .. }) => id,
124 other => panic!("expected a note, got {other:?}"),
125 }
126}
127
128fn text(fx: &Fx, id: u64) -> serde_json::Value {
129 match fx.b.recall(&[id], None) {
130 ControlResponse::ComposeRecalled(r) => {
131 serde_json::from_str(r.cited[0].text.as_deref().unwrap()).unwrap()
132 }
133 other => panic!("recall: {other:?}"),
134 }
135}
136
137/// Move the real `d` aside and put a symlink to `outside` in its place.
138fn swap_in_symlink(ws: PathBuf, outside: PathBuf) {
139 std::fs::rename(ws.join("d"), ws.join("d.held")).unwrap();
140 std::os::unix::fs::symlink(&outside, ws.join("d")).unwrap();
141}
142
143/// authorize: `d` is swapped for a symlink to outside bytes after the
144/// confinement check and before the read. The grant must record the hash of the
145/// inside file (what confinement approved), never the outside bytes.
146#[test]
147#[cfg_attr(not(compose_linked), ignore = "needs the linked librx_compose.a")]
148fn mint_records_the_prior_of_the_confined_file() {
149 let _home = home_guard::home_slot();
150 let Some(fx) = Fx::new() else { return };
151 let outside = fx.root.join("outside");
152 std::fs::create_dir_all(&outside).unwrap();
153 std::fs::write(fx.file(), "inside").unwrap();
154 std::fs::write(outside.join("NOTES.md"), "expected").unwrap();
155 let (ws, o) = (fx.ws.clone(), outside.clone());
156 test_hooks::set_pause(move || swap_in_symlink(ws, o));
157 let g = id(effects::mint_grant(&fx.b, &fx.mint_req()));
158 assert!(
159 fx.ws.join("d").is_symlink(),
160 "the swap hook did not fire inside the call"
161 );
162 assert_eq!(
163 text(&fx, g)["prior_sha256"],
164 serde_json::json!(sha(b"inside")),
165 "the grant recorded bytes that confinement did not check"
166 );
167}
168
169/// open_intent: the grant was minted on "old\n". `d` is then a symlink to an
170/// outside directory that holds the SAME bytes, while the real `d` (now
171/// holding different bytes) is parked. The swap back to the real directory
172/// fires at the start of the confined open. The read must see the real file,
173/// find it changed, and refuse as Stale with no intent recorded. A by-path read
174/// done before the swap would read the outside "old\n", match the grant and
175/// record an intent.
176#[test]
177#[cfg_attr(not(compose_linked), ignore = "needs the linked librx_compose.a")]
178fn open_intent_reads_the_confined_file_not_the_path() {
179 let _home = home_guard::home_slot();
180 let Some(fx) = Fx::new() else { return };
181 let outside = fx.root.join("outside");
182 std::fs::create_dir_all(&outside).unwrap();
183 std::fs::write(fx.file(), "old\n").unwrap();
184 let g = id(effects::mint_grant(&fx.b, &fx.mint_req()));
185 std::fs::write(outside.join("NOTES.md"), "old\n").unwrap();
186 std::fs::write(fx.file(), "changed\n").unwrap();
187 swap_in_symlink(fx.ws.clone(), outside);
188 let ws = fx.ws.clone();
189 test_hooks::set_before(move || {
190 std::fs::remove_file(ws.join("d")).unwrap();
191 std::fs::rename(ws.join("d.held"), ws.join("d")).unwrap();
192 });
193 let r = effects::open_intent(&fx.b, &fx.req(g, false));
194 assert!(
195 !fx.ws.join("d").is_symlink(),
196 "the swap hook did not fire inside the call"
197 );
198 match &r {
199 ControlResponse::Error(e) => assert!(
200 e.starts_with("EFFECT_REFUSED Stale"),
201 "wanted Stale, got {e}"
202 ),
203 other => panic!("wanted a Stale refusal, got {other:?}"),
204 }
205}