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 / np1_v6_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 | //! NEXT-PHASE-1 v6 runtime cuts (ACCEPTANCE-v6 Section 6): the length-cut |
| 17 | //! refusal (N2), the token ids in the attempt record (R1) and the edit-mode |
| 18 | //! prompt block (T5). A deterministic scripted proposer; no model. |
| 19 | //! |
| 20 | //! Every test drives `ComposeBridge::run_task` (tests/support/compose_harness.rs) |
| 21 | //! and reads the prompts the scripted model was handed, which proposer was |
| 22 | //! asked, the refusal text of a goal refused before the model, and the run's |
| 23 | //! attempt record. |
| 24 | #[path = "support/compose_harness.rs"] |
| 25 | mod compose_harness; |
| 26 | #[path = "support/home_guard.rs"] |
| 27 | mod home_guard; |
| 28 | |
| 29 | use aien_runtime::{token_ids_sha256, Generation}; |
| 30 | use compose_harness::{silent, Asked, Call, Harness}; |
| 31 | use std::sync::Arc; |
| 32 | |
| 33 | /// A plain new-document goal whose destination is NOTES.md. |
| 34 | const GOAL: &str = "write a short note into NOTES.md"; |
| 35 | /// Proposals per task. |
| 36 | const MAX_ATTEMPTS: usize = 3; |
| 37 | /// Largest existing file the edit block carries, in bytes. |
| 38 | const EDIT_MAX_BYTES: usize = 8192; |
| 39 | |
| 40 | /// The NEXT-PHASE-1 seed workspace: README.md and docs/plan.txt. |
| 41 | fn seed(h: &Harness) { |
| 42 | h.write( |
| 43 | "README.md", |
| 44 | "# Demo project\n\nA small local project used by the NEXT-PHASE-1 campaign.\n", |
| 45 | ); |
| 46 | h.write("docs/plan.txt", "Plan: keep notes short and local.\n"); |
| 47 | } |
| 48 | |
| 49 | /// The fixed proposal template every prompt starts with (the product's text, |
| 50 | /// verbatim; the workspace is named by its logical label). |
| 51 | fn template(goal: &str) -> String { |
| 52 | format!( |
| 53 | "Goal: {goal}\nAuthorized workspace: workspace\n\ |
| 54 | Propose exactly one file change inside the workspace.\n\ |
| 55 | Answer in exactly this format and nothing else:\n\ |
| 56 | filename: <relative path>\n\ |
| 57 | <the complete new file content>" |
| 58 | ) |
| 59 | } |
| 60 | |
| 61 | /// The new-document block (the product's text, verbatim). |
| 62 | fn new_block(path: &str) -> String { |
| 63 | format!("\nWrite the new file {path}. The filename line of your answer must be exactly: filename: {path}") |
| 64 | } |
| 65 | |
| 66 | /// The edit block (the product's text, verbatim). |
| 67 | fn edit_block(path: &str, content: &str) -> String { |
| 68 | let nl = if content.ends_with('\n') { "" } else { "\n" }; |
| 69 | format!( |
| 70 | "\nThe file {path} already exists. Its current content is:\n{content}{nl}\ |
| 71 | Write the complete new content of {path}: keep every existing line and make the requested change." |
| 72 | ) |
| 73 | } |
| 74 | |
| 75 | /// The first model call of a run of `goal`, or the refusal text when the |
| 76 | /// bridge refused the goal before any model call. |
| 77 | fn first(h: &Harness, goal: &str) -> Result<Call, String> { |
| 78 | h.clear(); |
| 79 | match h.run(goal) { |
| 80 | Err(e) => { |
| 81 | assert!( |
| 82 | h.calls().is_empty(), |
| 83 | "{goal}: a refused goal reached the model" |
| 84 | ); |
| 85 | Err(e) |
| 86 | } |
| 87 | Ok(r) => Ok(h |
| 88 | .calls() |
| 89 | .into_iter() |
| 90 | .next() |
| 91 | .unwrap_or_else(|| panic!("{goal}: no model call: {r:?}"))), |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | /// N2: a reply that stopped at the token limit is refused even when it |
| 96 | /// parses; every attempt is recorded with its stop reason and nothing passes. |
| 97 | #[test] |
| 98 | #[cfg_attr(not(compose_linked), ignore = "needs librx_compose.a: stub build")] |
| 99 | fn length_cut_reply_is_refused_even_when_it_parses() { |
| 100 | let _home = home_guard::home_slot(); |
| 101 | // Calls 1..=3 (the first run) stop at the token limit; call 4 (the second |
| 102 | // run) at eos; call 5 (the third run) with an unknown stop reason. |
| 103 | let h = Harness::new(Arc::new(|n, _, _| { |
| 104 | Ok(Generation { |
| 105 | text: "filename: NOTES.md\nkeep every change\n".into(), |
| 106 | tokens: 4, |
| 107 | finish_reason: match n { |
| 108 | 1..=3 => Some("max_tokens".into()), |
| 109 | 4 => Some("eos".into()), |
| 110 | _ => None, |
| 111 | }, |
| 112 | ..Default::default() |
| 113 | }) |
| 114 | })); |
| 115 | let r = h.run(GOAL).unwrap(); |
| 116 | let why = r.proposer_error.clone().unwrap(); |
| 117 | assert!(why.contains("finish_reason max_tokens"), "{why}"); |
| 118 | assert_eq!((r.proposal, r.uncommitted_proposal), (None, None)); |
| 119 | let a = &r.proposal_attempts; |
| 120 | assert_eq!(a.len(), MAX_ATTEMPTS); |
| 121 | for x in a { |
| 122 | assert_eq!(x.outcome, "refused"); |
| 123 | assert_eq!(x.finish_reason.as_deref(), Some("max_tokens")); |
| 124 | assert!(x.reason.as_deref().unwrap().contains("token limit")); |
| 125 | } |
| 126 | // eos and an unknown stop reason are not length cuts: the same reply parses. |
| 127 | for want in [Some("eos"), None] { |
| 128 | let r = h.run(GOAL).unwrap(); |
| 129 | let a = &r.proposal_attempts; |
| 130 | assert_eq!(a.len(), 1, "{a:?}"); |
| 131 | assert_eq!(a[0].outcome, "parsed", "{a:?}"); |
| 132 | assert_eq!(a[0].reason, None); |
| 133 | assert_eq!(a[0].finish_reason.as_deref(), want); |
| 134 | assert_eq!(r.proposer_error, None); |
| 135 | } |
| 136 | assert_eq!(h.calls().len(), MAX_ATTEMPTS + 2); |
| 137 | } |
| 138 | |
| 139 | /// R1: the generated ids, the prompt id count and the prompt ids digest |
| 140 | /// reach the attempt record unchanged; an older record without them reads. |
| 141 | #[test] |
| 142 | #[cfg_attr(not(compose_linked), ignore = "needs librx_compose.a: stub build")] |
| 143 | fn token_ids_reach_the_attempt_record() { |
| 144 | let _home = home_guard::home_slot(); |
| 145 | let ids = vec![63045u32, 22030, 198]; |
| 146 | let p = vec![128000u32, 128006, 882]; |
| 147 | let (ids2, p2) = (ids.clone(), p.clone()); |
| 148 | let h = Harness::new(Arc::new(move |_, _, _| { |
| 149 | Ok(Generation { |
| 150 | text: "filename: NOTES.md\nok\n".into(), |
| 151 | tokens: 3, |
| 152 | finish_reason: Some("eos".into()), |
| 153 | token_ids: Some(ids2.clone()), |
| 154 | prompt_tokens: Some(p2.len()), |
| 155 | prompt_ids_sha256: Some(token_ids_sha256(&p2)), |
| 156 | decoding: None, |
| 157 | ops: None, |
| 158 | }) |
| 159 | })); |
| 160 | let r = h.run(GOAL).unwrap(); |
| 161 | assert_eq!(r.proposer_error, None); |
| 162 | let a = &r.proposal_attempts; |
| 163 | assert_eq!(a[0].outcome, "parsed"); |
| 164 | assert_eq!(a[0].token_ids.as_deref(), Some(ids.as_slice())); |
| 165 | assert_eq!(a[0].prompt_tokens, Some(3)); |
| 166 | assert_eq!( |
| 167 | a[0].prompt_ids_sha256.as_deref(), |
| 168 | Some(token_ids_sha256(&p).as_str()) |
| 169 | ); |
| 170 | // 4 little-endian bytes per id. |
| 171 | let mut bytes = Vec::new(); |
| 172 | for x in &p { |
| 173 | bytes.extend_from_slice(&x.to_le_bytes()); |
| 174 | } |
| 175 | use sha2::Digest; |
| 176 | let want: String = sha2::Sha256::digest(&bytes) |
| 177 | .iter() |
| 178 | .map(|b| format!("{b:02x}")) |
| 179 | .collect(); |
| 180 | assert_eq!(token_ids_sha256(&p), want); |
| 181 | let old = r#"{"attempt":1,"ms":1,"tokens":48,"outcome":"parsed","reason":null, |
| 182 | "text_sha256":null,"text":null,"aegis":null,"finish_reason":"eos"}"#; |
| 183 | let r: aien_runtime::control::ProposalAttempt = serde_json::from_str(old).unwrap(); |
| 184 | assert_eq!( |
| 185 | (r.token_ids, r.prompt_tokens, r.prompt_ids_sha256), |
| 186 | (None, None, None) |
| 187 | ); |
| 188 | } |
| 189 | |
| 190 | /// T5: the v5 goals on the v5 seed keep the v5 prompt byte for byte; a goal |
| 191 | /// naming an existing file gets the block with that file's content. |
| 192 | #[test] |
| 193 | #[cfg_attr(not(compose_linked), ignore = "needs librx_compose.a: stub build")] |
| 194 | fn edit_block_only_when_the_goal_names_an_existing_file() { |
| 195 | let _home = home_guard::home_slot(); |
| 196 | let h = Harness::new(silent()); |
| 197 | seed(&h); |
| 198 | // A new-document goal keeps the v5 prompt and gains the destination line |
| 199 | // (issue #288); an unsafe destination is refused before any model call. |
| 200 | for (goal, dest) in [ |
| 201 | ("Create the file NOTES.md with a short plain-text note that says the project keeps every change inside its workspace.", Some("NOTES.md")), |
| 202 | ("Create the file docs/CONTACT.txt with one line giving the maintainer name Ada Lovelace and the email ada@example.org.", Some("docs/CONTACT.txt")), |
| 203 | ("Create the file TODO.md listing three tasks: write tests, update the changelog, tag the release.", Some("TODO.md")), |
| 204 | ("Create the file ../outside.txt with the single line: written outside the workspace.", None), |
| 205 | ("Create the file docs/GUIDE.md about docs and plan.txt.", Some("docs/GUIDE.md")), |
| 206 | ] { |
| 207 | let Some(d) = dest else { |
| 208 | let e = first(&h, goal).expect_err(goal); |
| 209 | assert!(e.contains("destination ../outside.txt"), "{goal}: {e}"); |
| 210 | continue; |
| 211 | }; |
| 212 | let c = first(&h, goal).unwrap_or_else(|e| panic!("{goal}: refused: {e}")); |
| 213 | assert_eq!(c.asked, Asked::Document, "{goal}"); |
| 214 | assert_eq!(c.prompt, format!("{}{}", template(goal), new_block(d)), "{goal}"); |
| 215 | } |
| 216 | let log = "# Changelog\n\n## 0.1.0\n- initial release\n"; |
| 217 | h.write("CHANGELOG.md", log); |
| 218 | let goal = |
| 219 | "Add the line \"- add contact file\" under the \"## 0.1.0\" heading in CHANGELOG.md."; |
| 220 | let c = first(&h, goal).unwrap(); |
| 221 | assert_eq!(c.asked, Asked::Edit); |
| 222 | let p = c.prompt; |
| 223 | assert_eq!( |
| 224 | p, |
| 225 | format!("{}{}", template(goal), edit_block("CHANGELOG.md", log)) |
| 226 | ); |
| 227 | assert!(p.contains("\nThe file CHANGELOG.md already exists. Its current content is:\n# Changelog\n\n## 0.1.0\n- initial release\nWrite the complete new content of CHANGELOG.md")); |
| 228 | // Quoted and nested paths are found; the first existing one wins. |
| 229 | let c = first(&h, "Edit `docs/plan.txt`, then README.md").unwrap(); |
| 230 | assert_eq!(c.asked, Asked::Edit); |
| 231 | assert!( |
| 232 | c.prompt.ends_with(&edit_block( |
| 233 | "docs/plan.txt", |
| 234 | "Plan: keep notes short and local.\n" |
| 235 | )), |
| 236 | "{}", |
| 237 | c.prompt |
| 238 | ); |
| 239 | } |
| 240 | |
| 241 | /// T5 boundary: the block never reads outside the workspace, a directory, |
| 242 | /// a symlink leaving the workspace, a non-UTF-8 file or an oversized file. |
| 243 | /// The offline re-derivation (`compose::rederive::task_prompt`, used by |
| 244 | /// examples/np1_reference.rs) keeps the plain v5 prompt, byte for byte, for a |
| 245 | /// goal whose destination is unsafe. The bridge itself refuses that goal |
| 246 | /// before any model call (`edit_block_only_when_the_goal_names_an_existing_file`). |
| 247 | #[test] |
| 248 | fn rederive_keeps_the_plain_prompt_for_an_unsafe_destination() { |
| 249 | use aien_runtime::compose::rederive::{proposal_prompt, task_prompt}; |
| 250 | let d = tempfile::tempdir().unwrap(); |
| 251 | let goal = |
| 252 | "Create the file ../outside.txt with the single line: written outside the workspace."; |
| 253 | assert_eq!(proposal_prompt(goal), template(goal)); |
| 254 | assert_eq!(task_prompt(goal, d.path()), template(goal)); |
| 255 | } |
| 256 | |
| 257 | #[test] |
| 258 | #[cfg_attr(not(compose_linked), ignore = "needs librx_compose.a: stub build")] |
| 259 | fn edit_block_never_reads_outside_or_oversized() { |
| 260 | let _home = home_guard::home_slot(); |
| 261 | let h = Harness::new(silent()); |
| 262 | seed(&h); |
| 263 | let d = h.tmp.path(); |
| 264 | std::fs::write(d.join("secret.txt"), "outside\n").unwrap(); |
| 265 | std::os::unix::fs::symlink(d.join("secret.txt"), h.ws().join("link.txt")).unwrap(); |
| 266 | std::fs::write(h.ws().join("bin.dat"), [0xffu8, 0xfe, 0x00]).unwrap(); |
| 267 | h.write("big.txt", &"x".repeat(EDIT_MAX_BYTES + 1)); |
| 268 | for goal in [ |
| 269 | "Edit ../secret.txt now", |
| 270 | &format!("Edit {}", d.join("secret.txt").display()), |
| 271 | "Edit link.txt now", |
| 272 | "Edit docs now", |
| 273 | "Edit bin.dat now", |
| 274 | "Edit big.txt now", |
| 275 | "Edit ~/secret.txt now", |
| 276 | ] { |
| 277 | // Refused before the model, or a new document: never an edit target. |
| 278 | match first(&h, goal) { |
| 279 | Ok(c) => { |
| 280 | assert_eq!(c.asked, Asked::Document, "{goal}"); |
| 281 | assert!(!c.prompt.contains("already exists"), "{goal}: {}", c.prompt); |
| 282 | } |
| 283 | Err(e) => assert!(e.starts_with("RunComposeTask: destination "), "{goal}: {e}"), |
| 284 | } |
| 285 | } |
| 286 | } |