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 / inputs_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 | //! sc#382 and sc#383 items 2 and 3 (WHOLE-SYSTEM-E2E, arch#190): input files the |
| 17 | //! goal points at are shown to the model and never written, and "named X in the |
| 18 | //! Y folder" resolves to `Y/X`. Pre-registered in |
| 19 | //! `docs/campaigns/whole-system-e2e/REGRESSION-sc382-sc383.md`. |
| 20 | //! |
| 21 | //! Every test drives `ComposeBridge::run_task` (tests/support/compose_harness.rs): |
| 22 | //! the destination is the new-document or edit block the prompt ends with, |
| 23 | //! the inputs are the prompt's input block and the report's `inputs` and |
| 24 | //! `inputs_omitted`, and a refused goal is the bridge's refusal text. |
| 25 | #[path = "support/compose_harness.rs"] |
| 26 | mod compose_harness; |
| 27 | #[path = "support/home_guard.rs"] |
| 28 | mod home_guard; |
| 29 | |
| 30 | use aien_runtime::control::{ComposeTaskReport, ControlResponse}; |
| 31 | use aien_runtime::Generation; |
| 32 | use compose_harness::{silent, Asked, Harness, Script}; |
| 33 | use sha2::{Digest, Sha256}; |
| 34 | use std::sync::Arc; |
| 35 | |
| 36 | // The three objective texts of scripts/whole_system_e2e.sh, verbatim. |
| 37 | const OBJ: &str = "Read the three text files in the inbox folder and write a Markdown report named report.md in the outbox folder. Start with a Markdown heading line that begins with \"# \". Use at most 200 words. Mention each file by name."; |
| 38 | const OBJ6: &str = "Write a file named done.md in the outbox folder containing one line: finished."; |
| 39 | const OBJC: &str = "Write a file named ctrl.md in the outbox folder containing one line: control."; |
| 40 | // The first mechanics objective of harness v1 (sc#383 item 2). |
| 41 | const OBJ_NAMED: &str = "Write a Markdown report named report.md about the three inbox files named harbour-notes.txt, orchard-ledger.txt and windmill-log.txt. Start with a Markdown heading line that begins with \"# \". Use at most 200 words. Mention each file by name."; |
| 42 | |
| 43 | /// Total bytes of input text a prompt carries (the product's limit). |
| 44 | const INPUTS_MAX_BYTES: usize = 16384; |
| 45 | |
| 46 | const FILES: [(&str, &str); 3] = [ |
| 47 | ( |
| 48 | "harbour-notes.txt", |
| 49 | "Harbour notes. Three boats came in before noon.\n", |
| 50 | ), |
| 51 | ("orchard-ledger.txt", "Orchard ledger. Forty apple trees.\n"), |
| 52 | ( |
| 53 | "windmill-log.txt", |
| 54 | "Windmill log. The brake band was replaced.\n", |
| 55 | ), |
| 56 | ]; |
| 57 | |
| 58 | /// A harness whose model runs `script` and whose workspace holds the inbox |
| 59 | /// and an empty outbox. |
| 60 | fn inbox(script: Script) -> Harness { |
| 61 | let h = Harness::new(script); |
| 62 | std::fs::create_dir_all(h.ws().join("inbox")).unwrap(); |
| 63 | std::fs::create_dir_all(h.ws().join("outbox")).unwrap(); |
| 64 | // Written out of order on purpose: the order must come from the names. |
| 65 | for (n, c) in FILES.iter().rev() { |
| 66 | h.write(&format!("inbox/{n}"), c); |
| 67 | } |
| 68 | h |
| 69 | } |
| 70 | |
| 71 | /// `inbox` with a silent model. |
| 72 | fn ws_with_inbox() -> Harness { |
| 73 | inbox(silent()) |
| 74 | } |
| 75 | |
| 76 | fn sha(s: &str) -> String { |
| 77 | Sha256::digest(s.as_bytes()) |
| 78 | .iter() |
| 79 | .map(|b| format!("{b:02x}")) |
| 80 | .collect() |
| 81 | } |
| 82 | |
| 83 | /// The first prompt of a run of `goal` and the run's report. |
| 84 | fn first(h: &Harness, goal: &str) -> (String, ComposeTaskReport) { |
| 85 | h.clear(); |
| 86 | let r = h |
| 87 | .run(goal) |
| 88 | .unwrap_or_else(|e| panic!("{goal}: refused: {e}")); |
| 89 | let c = h.calls(); |
| 90 | let p = c |
| 91 | .first() |
| 92 | .unwrap_or_else(|| panic!("{goal}: no model call: {r:?}")) |
| 93 | .prompt |
| 94 | .clone(); |
| 95 | (p, r) |
| 96 | } |
| 97 | |
| 98 | /// The bridge's refusal of `goal`; no model call was made. |
| 99 | fn refused(h: &Harness, goal: &str) -> String { |
| 100 | h.clear(); |
| 101 | let e = h.run(goal).expect_err(goal); |
| 102 | assert!( |
| 103 | h.calls().is_empty(), |
| 104 | "{goal}: a refused goal reached the model" |
| 105 | ); |
| 106 | e |
| 107 | } |
| 108 | |
| 109 | /// The new-document block the prompt ends with when the goal names a |
| 110 | /// destination that does not exist yet (the product's text, verbatim). |
| 111 | fn new_block(path: &str) -> String { |
| 112 | format!("\nWrite the new file {path}. The filename line of your answer must be exactly: filename: {path}") |
| 113 | } |
| 114 | |
| 115 | /// The fixed proposal template every prompt starts with (the product's text, |
| 116 | /// verbatim; the workspace is named by its logical label). |
| 117 | fn template(goal: &str) -> String { |
| 118 | format!( |
| 119 | "Goal: {goal}\nAuthorized workspace: workspace\n\ |
| 120 | Propose exactly one file change inside the workspace.\n\ |
| 121 | Answer in exactly this format and nothing else:\n\ |
| 122 | filename: <relative path>\n\ |
| 123 | <the complete new file content>" |
| 124 | ) |
| 125 | } |
| 126 | |
| 127 | /// Asserts the destination of `goal` is the new document `want`. |
| 128 | fn assert_new_dest(h: &Harness, goal: &str, want: &str) { |
| 129 | let (p, _) = first(h, goal); |
| 130 | assert!(p.ends_with(&new_block(want)), "{goal}: {p}"); |
| 131 | } |
| 132 | |
| 133 | #[test] |
| 134 | #[cfg_attr(not(compose_linked), ignore = "needs librx_compose.a: stub build")] |
| 135 | fn folder_phrase_resolves_done_ctrl_and_report_destinations() { |
| 136 | let _home = home_guard::home_slot(); |
| 137 | let h = ws_with_inbox(); |
| 138 | assert_new_dest(&h, OBJ6, "outbox/done.md"); |
| 139 | assert_new_dest(&h, OBJC, "outbox/ctrl.md"); |
| 140 | assert_new_dest(&h, OBJ, "outbox/report.md"); |
| 141 | // The model is shown the resolved path. |
| 142 | let (prompt, _) = first(&h, OBJC); |
| 143 | assert!(prompt.contains("filename: outbox/ctrl.md"), "{prompt}"); |
| 144 | } |
| 145 | |
| 146 | #[test] |
| 147 | #[cfg_attr(not(compose_linked), ignore = "needs librx_compose.a: stub build")] |
| 148 | fn folder_phrase_works_without_the_article_and_for_directory() { |
| 149 | let _home = home_guard::home_slot(); |
| 150 | let h = ws_with_inbox(); |
| 151 | assert_new_dest( |
| 152 | &h, |
| 153 | "Write a file named a.md in outbox folder containing x.", |
| 154 | "outbox/a.md", |
| 155 | ); |
| 156 | assert_new_dest( |
| 157 | &h, |
| 158 | "Write a file named b.md in the outbox directory containing x.", |
| 159 | "outbox/b.md", |
| 160 | ); |
| 161 | } |
| 162 | |
| 163 | #[test] |
| 164 | #[cfg_attr(not(compose_linked), ignore = "needs librx_compose.a: stub build")] |
| 165 | fn input_names_are_not_write_targets() { |
| 166 | let _home = home_guard::home_slot(); |
| 167 | let h = ws_with_inbox(); |
| 168 | assert_new_dest(&h, OBJ_NAMED, "report.md"); |
| 169 | } |
| 170 | |
| 171 | #[test] |
| 172 | fn two_distinct_write_targets_stay_ambiguous() { |
| 173 | let _home = home_guard::home_slot(); |
| 174 | let h = ws_with_inbox(); |
| 175 | let e = refused(&h, "Create alpha.md and beta.md about the weather."); |
| 176 | assert!(e.contains("ambiguous destination"), "{e}"); |
| 177 | // A name that does not exist is not an input: two new files are still two targets. |
| 178 | let e = refused( |
| 179 | &h, |
| 180 | "Write a report named one.md and write another named two.md about the inbox folder.", |
| 181 | ); |
| 182 | assert!(e.contains("ambiguous destination"), "{e}"); |
| 183 | } |
| 184 | |
| 185 | #[test] |
| 186 | #[cfg_attr(not(compose_linked), ignore = "needs librx_compose.a: stub build")] |
| 187 | fn existing_destination_is_still_an_edit() { |
| 188 | let _home = home_guard::home_slot(); |
| 189 | let h = ws_with_inbox(); |
| 190 | h.write("README.md", "# r\n"); |
| 191 | let c = { |
| 192 | h.clear(); |
| 193 | h.run("Append a line to README.md").unwrap(); |
| 194 | h.calls().remove(0) |
| 195 | }; |
| 196 | // The edit target, and so the destination, is README.md. |
| 197 | assert_eq!(c.asked, Asked::Edit); |
| 198 | assert!( |
| 199 | c.prompt |
| 200 | .contains("\nThe file README.md already exists. Its current content is:\n# r\n"), |
| 201 | "{}", |
| 202 | c.prompt |
| 203 | ); |
| 204 | } |
| 205 | |
| 206 | #[test] |
| 207 | #[cfg_attr(not(compose_linked), ignore = "needs librx_compose.a: stub build")] |
| 208 | fn folder_source_phrase_includes_inputs_sorted_with_digests() { |
| 209 | let _home = home_guard::home_slot(); |
| 210 | let h = ws_with_inbox(); |
| 211 | let (prompt, r) = first(&h, OBJ); |
| 212 | let (inputs, omitted) = (r.inputs, r.inputs_omitted); |
| 213 | assert!(prompt.ends_with(&new_block("outbox/report.md")), "{prompt}"); |
| 214 | assert!(omitted.is_empty(), "{omitted:?}"); |
| 215 | let names: Vec<&str> = inputs.iter().map(|i| i.name.as_str()).collect(); |
| 216 | assert_eq!( |
| 217 | names, |
| 218 | [ |
| 219 | "inbox/harbour-notes.txt", |
| 220 | "inbox/orchard-ledger.txt", |
| 221 | "inbox/windmill-log.txt" |
| 222 | ] |
| 223 | ); |
| 224 | let mut at = 0; |
| 225 | for ((n, c), i) in FILES.iter().zip(&inputs) { |
| 226 | assert_eq!((i.sha256.clone(), i.bytes), (sha(c), c.len())); |
| 227 | let p = prompt[at..] |
| 228 | .find(c) |
| 229 | .unwrap_or_else(|| panic!("{n} missing: {prompt}")); |
| 230 | at += p; |
| 231 | assert!(prompt.contains(&format!("inbox/{n}"))); |
| 232 | } |
| 233 | // The inputs are not the file to write. |
| 234 | assert!(prompt.contains("filename: outbox/report.md")); |
| 235 | } |
| 236 | |
| 237 | #[test] |
| 238 | #[cfg_attr(not(compose_linked), ignore = "needs librx_compose.a: stub build")] |
| 239 | fn named_files_become_inputs_but_destination_is_not() { |
| 240 | let _home = home_guard::home_slot(); |
| 241 | let h = ws_with_inbox(); |
| 242 | // The goal names the files bare and says where they live. |
| 243 | let goal = "Write a Markdown report named report.md in the outbox folder about harbour-notes.txt, orchard-ledger.txt and windmill-log.txt in the inbox folder."; |
| 244 | let (prompt, r) = first(&h, goal); |
| 245 | assert!(prompt.ends_with(&new_block("outbox/report.md")), "{prompt}"); |
| 246 | assert_eq!(r.inputs.len(), 3, "{:?}", r.inputs); |
| 247 | assert!(prompt.contains("Windmill log. The brake band was replaced.")); |
| 248 | // A bare name that exists at the workspace root is an input too. |
| 249 | h.write("facts.txt", "Fact one.\n"); |
| 250 | let (prompt, r) = first(&h, "Write summary.md based on the notes named facts.txt"); |
| 251 | assert!(prompt.ends_with(&new_block("summary.md")), "{prompt}"); |
| 252 | assert_eq!( |
| 253 | r.inputs.iter().map(|i| i.name.as_str()).collect::<Vec<_>>(), |
| 254 | ["facts.txt"] |
| 255 | ); |
| 256 | } |
| 257 | |
| 258 | #[test] |
| 259 | #[cfg_attr(not(compose_linked), ignore = "needs librx_compose.a: stub build")] |
| 260 | fn inputs_are_capped_and_omissions_are_listed() { |
| 261 | let _home = home_guard::home_slot(); |
| 262 | let h = ws_with_inbox(); |
| 263 | let big = "x".repeat(INPUTS_MAX_BYTES / 2 + 1); |
| 264 | h.write("inbox/big1.txt", &big); |
| 265 | h.write("inbox/big2.txt", &big); |
| 266 | let (_, r) = first(&h, OBJ); |
| 267 | let (inputs, omitted) = (r.inputs, r.inputs_omitted); |
| 268 | let total: usize = inputs.iter().map(|i| i.bytes).sum(); |
| 269 | assert!(total <= INPUTS_MAX_BYTES, "{total}"); |
| 270 | assert!(inputs.iter().any(|i| i.name == "inbox/big1.txt")); |
| 271 | assert!( |
| 272 | omitted.iter().any(|o| o.name == "inbox/big2.txt"), |
| 273 | "{omitted:?}" |
| 274 | ); |
| 275 | // Deterministic: the same goal gives the same lists. |
| 276 | let (_, again) = first(&h, OBJ); |
| 277 | assert_eq!((again.inputs, again.inputs_omitted), (inputs, omitted)); |
| 278 | } |
| 279 | |
| 280 | #[test] |
| 281 | #[cfg_attr(not(compose_linked), ignore = "needs librx_compose.a: stub build")] |
| 282 | fn non_utf8_and_non_regular_inputs_are_omitted_not_read() { |
| 283 | let _home = home_guard::home_slot(); |
| 284 | let h = ws_with_inbox(); |
| 285 | std::fs::write(h.ws().join("inbox").join("bin.dat"), [0xff, 0xfe, 0x00]).unwrap(); |
| 286 | std::fs::create_dir(h.ws().join("inbox").join("sub")).unwrap(); |
| 287 | let out = tempfile::tempdir().unwrap(); |
| 288 | std::fs::write(out.path().join("secret.txt"), "TOPSECRET").unwrap(); |
| 289 | std::os::unix::fs::symlink( |
| 290 | out.path().join("secret.txt"), |
| 291 | h.ws().join("inbox").join("leak.txt"), |
| 292 | ) |
| 293 | .unwrap(); |
| 294 | let (prompt, r) = first(&h, OBJ); |
| 295 | assert!(!prompt.contains("TOPSECRET")); |
| 296 | assert!(r |
| 297 | .inputs |
| 298 | .iter() |
| 299 | .all(|i| i.name != "inbox/bin.dat" && i.name != "inbox/leak.txt")); |
| 300 | for n in ["inbox/bin.dat", "inbox/leak.txt"] { |
| 301 | assert!( |
| 302 | r.inputs_omitted.iter().any(|o| o.name == n), |
| 303 | "{n}: {:?}", |
| 304 | r.inputs_omitted |
| 305 | ); |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | // Refused before any model call: runs in the stub build too. |
| 310 | #[test] |
| 311 | fn named_input_escaping_workspace_is_refused_before_model() { |
| 312 | let _home = home_guard::home_slot(); |
| 313 | let h = ws_with_inbox(); |
| 314 | std::fs::write(h.tmp.path().join("outside.txt"), "TOPSECRET").unwrap(); |
| 315 | std::os::unix::fs::symlink(h.tmp.path().join("outside.txt"), h.ws().join("link.txt")).unwrap(); |
| 316 | for goal in [ |
| 317 | "Write report.md about ../outside.txt", |
| 318 | "Write report.md about /etc/hostname", |
| 319 | "Write report.md about link.txt", |
| 320 | ] { |
| 321 | let e = refused(&h, goal); |
| 322 | assert!( |
| 323 | e.contains("RunComposeTask") && e.contains("input"), |
| 324 | "{goal}: {e}" |
| 325 | ); |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | #[test] |
| 330 | #[cfg_attr(not(compose_linked), ignore = "needs librx_compose.a: stub build")] |
| 331 | fn goal_without_inputs_keeps_prompt_byte_for_byte() { |
| 332 | let _home = home_guard::home_slot(); |
| 333 | let h = ws_with_inbox(); |
| 334 | let goal = "Write a file named notes.md containing one line: hello."; |
| 335 | let (prompt, r) = first(&h, goal); |
| 336 | assert!(r.inputs.is_empty() && r.inputs_omitted.is_empty()); |
| 337 | assert!(!prompt.contains("Input files")); |
| 338 | assert_eq!( |
| 339 | prompt, |
| 340 | format!("{}{}", template(goal), new_block("notes.md")) |
| 341 | ); |
| 342 | } |
| 343 | |
| 344 | /// A model reply to the e2e objective: a report naming the three inputs. |
| 345 | const REPORT: &str = "filename: outbox/report.md\n# Report\nharbour-notes.txt, orchard-ledger.txt, windmill-log.txt\n"; |
| 346 | |
| 347 | #[test] |
| 348 | fn compose_propose_report_lists_inputs_with_digests() { |
| 349 | let _home = home_guard::home_slot(); |
| 350 | let h = inbox(Arc::new(|_, _, _| { |
| 351 | Ok(Generation { |
| 352 | text: REPORT.into(), |
| 353 | tokens: 9, |
| 354 | finish_reason: None, |
| 355 | ..Default::default() |
| 356 | }) |
| 357 | })); |
| 358 | let resp = h.bridge.run_task(OBJ, h.ws().to_str().unwrap()); |
| 359 | if !aien_omega_compose::LINKED { |
| 360 | match resp { |
| 361 | ControlResponse::Error(e) => assert!(e.contains("not linked"), "{e}"), |
| 362 | other => panic!("stub build must refuse, got {other:?}"), |
| 363 | } |
| 364 | return; |
| 365 | } |
| 366 | let ControlResponse::ComposeTaskResult(r) = resp else { |
| 367 | panic!("{resp:?}") |
| 368 | }; |
| 369 | assert!(h.calls()[0].prompt.contains("Windmill log.")); |
| 370 | assert_eq!(r.inputs.len(), 3); |
| 371 | for ((n, c), i) in FILES.iter().zip(&r.inputs) { |
| 372 | assert_eq!(i.name, format!("inbox/{n}")); |
| 373 | assert_eq!(i.sha256, sha(c)); |
| 374 | } |
| 375 | } |