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 / requirements_elided_bound_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 | //! #344: a second bound whose noun is left out ("at least 20 lines and no more |
| 17 | //! than 40") is read with the earlier noun, or reported UNCERTAIN. It is never |
| 18 | //! dropped silently. |
| 19 | #[path = "support/compose_harness.rs"] |
| 20 | mod compose_harness; |
| 21 | #[path = "support/home_guard.rs"] |
| 22 | mod home_guard; |
| 23 | |
| 24 | use aien_runtime::control::ControlResponse; |
| 25 | use aien_runtime::requirements::{analyze, refusal_reason, Requirement}; |
| 26 | use aien_runtime::{check_file_proposal, ComposeBridge, ComposeProposer, Generation}; |
| 27 | use compose_harness::{always, Harness}; |
| 28 | use std::sync::Arc; |
| 29 | use std::time::Duration; |
| 30 | |
| 31 | use Requirement::*; |
| 32 | |
| 33 | fn read(goal: &str) -> Vec<Requirement> { |
| 34 | let ex = analyze(goal); |
| 35 | assert!(ex.uncertain.is_empty(), "{goal:?}: {:?}", ex.uncertain); |
| 36 | ex.requirements |
| 37 | } |
| 38 | |
| 39 | #[test] |
| 40 | fn elided_max_after_min_is_read_with_the_earlier_noun() { |
| 41 | let want = [MinLines(20), MaxLines(40)]; |
| 42 | for g in [ |
| 43 | "Write at least 20 lines and no more than 40.", |
| 44 | "Write at least 20 lines and no more than 40", |
| 45 | "Write at least 20 lines, and no more than 40.", |
| 46 | "Write at least 20 lines but at most 40.", |
| 47 | "Write no fewer than 20 lines and up to 40.", |
| 48 | "Create docs/a.md with at least 20 lines and at most 40 in total.", |
| 49 | ] { |
| 50 | let r = read(g); |
| 51 | for w in &want { |
| 52 | assert!(r.contains(w), "{g:?} -> {r:?}"); |
| 53 | } |
| 54 | } |
| 55 | let r = read("Write at least 100 words but at most 200."); |
| 56 | assert!( |
| 57 | r.contains(&MinWords(100)) && r.contains(&MaxWords(200)), |
| 58 | "{r:?}" |
| 59 | ); |
| 60 | // strict cue keeps its adjustment: fewer than 40 is at most 39 |
| 61 | let r = read("Write at least 20 lines and fewer than 40."); |
| 62 | assert!(r.contains(&MaxLines(39)), "{r:?}"); |
| 63 | } |
| 64 | |
| 65 | #[test] |
| 66 | fn unsupported_noun_or_direction_is_uncertain_not_silent() { |
| 67 | // paragraphs have no upper bound in the model |
| 68 | let ex = analyze("Write at least 3 paragraphs and no more than 5."); |
| 69 | assert!(!ex.uncertain.is_empty(), "{ex:?}"); |
| 70 | // items: no upper bound either |
| 71 | let ex = analyze("List at least 3 items and no more than 5."); |
| 72 | assert!(!ex.uncertain.is_empty(), "{ex:?}"); |
| 73 | // a cue with no earlier noun to carry over |
| 74 | let ex = analyze("Write at least 20 lines and then keep it under, no more than 40."); |
| 75 | assert!(!ex.uncertain.is_empty(), "{ex:?}"); |
| 76 | } |
| 77 | |
| 78 | #[test] |
| 79 | fn ordinary_numbers_stay_silent() { |
| 80 | for g in [ |
| 81 | "Write at least 20 lines and fix 2 typos.", |
| 82 | "Fix 2 typos in README.md", |
| 83 | "Update section 3 of docs/a.md", |
| 84 | "Write at least 20 lines and update section 3.", |
| 85 | ] { |
| 86 | let r = analyze(g); |
| 87 | assert!(r.uncertain.is_empty(), "{g:?}: {:?}", r.uncertain); |
| 88 | assert!( |
| 89 | !r.requirements.iter().any(|x| matches!(x, MaxLines(_))), |
| 90 | "{g:?}: {:?}", |
| 91 | r.requirements |
| 92 | ); |
| 93 | } |
| 94 | assert!(analyze("Fix 2 typos").requirements.is_empty()); |
| 95 | assert!(analyze("Update section 3").requirements.is_empty()); |
| 96 | } |
| 97 | |
| 98 | fn gen(text: &str) -> Result<Generation, String> { |
| 99 | Ok(Generation { |
| 100 | text: text.to_string(), |
| 101 | tokens: 5, |
| 102 | finish_reason: Some("eos".into()), |
| 103 | ..Default::default() |
| 104 | }) |
| 105 | } |
| 106 | |
| 107 | fn reply(lines: usize) -> String { |
| 108 | let mut s = String::from("filename: docs/NOTES.md\n"); |
| 109 | for i in 0..lines { |
| 110 | s.push_str(&format!("line number {i}\n")); |
| 111 | } |
| 112 | s |
| 113 | } |
| 114 | |
| 115 | const GOAL: &str = "Create the file docs/NOTES.md with at least 20 lines and no more than 40."; |
| 116 | |
| 117 | #[test] |
| 118 | fn bytes_over_the_second_bound_are_refused_and_within_bounds_pass() { |
| 119 | let reqs = analyze(GOAL).requirements; |
| 120 | let over = check_file_proposal(&reply(45)).unwrap().content; |
| 121 | let why = refusal_reason(&reqs, &over).expect("45 lines must be refused"); |
| 122 | assert!(why.contains("40"), "{why}"); |
| 123 | for ok in [20usize, 30, 40] { |
| 124 | let c = check_file_proposal(&reply(ok)).unwrap().content; |
| 125 | assert!(refusal_reason(&reqs, &c).is_none(), "{ok} lines"); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | fn bridge(tmp: &std::path::Path, lines: usize) -> ComposeBridge { |
| 130 | let text = reply(lines); |
| 131 | let proposer: ComposeProposer = Arc::new(move |_: &str, _: Duration| gen(&text)); |
| 132 | ComposeBridge::new(tmp.join("compose"), proposer, "test:elided") |
| 133 | } |
| 134 | |
| 135 | fn report(resp: ControlResponse) -> Option<aien_runtime::control::ComposeTaskReport> { |
| 136 | if !aien_omega_compose::LINKED { |
| 137 | assert!(matches!(resp, ControlResponse::Error(_))); |
| 138 | return None; |
| 139 | } |
| 140 | let ControlResponse::ComposeTaskResult(r) = resp else { |
| 141 | panic!("{resp:?}") |
| 142 | }; |
| 143 | Some(*r) |
| 144 | } |
| 145 | |
| 146 | #[test] |
| 147 | fn linked_compose_does_not_commit_45_lines_but_commits_30() { |
| 148 | let _home = home_guard::home_slot(); |
| 149 | let tmp = tempfile::tempdir().unwrap(); |
| 150 | let ws = tmp.path().join("ws"); |
| 151 | std::fs::create_dir_all(&ws).unwrap(); |
| 152 | let r = report(bridge(tmp.path(), 45).run_task(GOAL, ws.to_str().unwrap())); |
| 153 | if let Some(r) = r { |
| 154 | assert!(!r.committed, "{r:?}"); |
| 155 | assert!(r.proposal.is_none()); |
| 156 | assert_eq!(r.proposal_attempts.len(), 3); |
| 157 | assert_eq!(std::fs::read_dir(&ws).unwrap().count(), 0); |
| 158 | } |
| 159 | let tmp = tempfile::tempdir().unwrap(); |
| 160 | let ws = tmp.path().join("ws"); |
| 161 | std::fs::create_dir_all(&ws).unwrap(); |
| 162 | if let Some(r) = report(bridge(tmp.path(), 30).run_task(GOAL, ws.to_str().unwrap())) { |
| 163 | assert!(r.committed, "{r:?}"); |
| 164 | let p = check_file_proposal(r.proposal.as_deref().unwrap()).unwrap(); |
| 165 | assert_eq!(p.content, check_file_proposal(&reply(30)).unwrap().content); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | /// The compose verification boundary through `ComposeBridge::run_task`: the |
| 170 | /// 45 line reply is refused on every attempt and nothing is approved; the 30 |
| 171 | /// line reply is approved byte for byte. |
| 172 | #[test] |
| 173 | #[cfg_attr(not(compose_linked), ignore = "needs librx_compose.a: stub build")] |
| 174 | fn compose_boundary_refuses_45_lines_and_approves_30() { |
| 175 | let _home = home_guard::home_slot(); |
| 176 | let ex = analyze(GOAL); |
| 177 | assert!(ex.uncertain.is_empty()); |
| 178 | let run = |lines: usize| { |
| 179 | let h = Harness::new(always(&reply(lines))); |
| 180 | h.run(GOAL).unwrap() |
| 181 | }; |
| 182 | let r = run(45); |
| 183 | assert!( |
| 184 | r.proposal.is_none() && r.uncommitted_proposal.is_none(), |
| 185 | "{r:?}" |
| 186 | ); |
| 187 | let why = r.proposer_error.unwrap(); |
| 188 | assert!(why.contains("40"), "{why}"); |
| 189 | let attempts = &r.proposal_attempts; |
| 190 | assert_eq!(attempts.len(), 3); |
| 191 | assert!(attempts |
| 192 | .iter() |
| 193 | .all(|a| a.outcome == "refused" && a.aegis.is_none())); |
| 194 | let r = run(30); |
| 195 | assert_eq!(r.proposal.as_deref(), Some(reply(30).as_str()), "{r:?}"); |
| 196 | assert_eq!(r.proposal_attempts.len(), 1); |
| 197 | } |
| 198 | |
| 199 | #[test] |
| 200 | fn bound_cue_with_bare_number_and_no_antecedent_is_uncertain() { |
| 201 | for g in [ |
| 202 | "Keep it to no more than 40", |
| 203 | "Keep it to no more than 40.", |
| 204 | "Write at least 20", |
| 205 | "Write at least 20.", |
| 206 | "Use up to 12.", |
| 207 | "Make it fewer than 40.", |
| 208 | "Make it more than 10.", |
| 209 | "Write a maximum of 30.", |
| 210 | "Write a minimum of 30.", |
| 211 | "Write no fewer than 5.", |
| 212 | ] { |
| 213 | let ex = analyze(g); |
| 214 | assert!(!ex.uncertain.is_empty(), "{g:?} silent: {ex:?}"); |
| 215 | } |
| 216 | for g in [ |
| 217 | "Fix 2 typos", |
| 218 | "Update section 3", |
| 219 | "Use Python 3", |
| 220 | "Use Python 3.", |
| 221 | ] { |
| 222 | let ex = analyze(g); |
| 223 | assert!(ex.uncertain.is_empty(), "{g:?}: {:?}", ex.uncertain); |
| 224 | assert!(ex.requirements.is_empty(), "{g:?}: {:?}", ex.requirements); |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | #[test] |
| 229 | fn every_bound_cue_with_a_number_is_read_or_uncertain() { |
| 230 | for g in [ |
| 231 | "Keep it at most 40 please", |
| 232 | "Keep it to no more than 40, thanks.", |
| 233 | "Include at least 2 of them", |
| 234 | "Fix at least 2 typos in README.md", |
| 235 | "Write at least 20 lines and fix at least 2 typos.", |
| 236 | "Use not more than 3 colours.", |
| 237 | "Use not less than 3 colours.", |
| 238 | ] { |
| 239 | let ex = analyze(g); |
| 240 | assert!(!ex.uncertain.is_empty(), "{g:?} silent: {ex:?}"); |
| 241 | } |
| 242 | // still read, not uncertain |
| 243 | assert_eq!(read("Write at most 40 lines please."), [MaxLines(40)]); |
| 244 | for g in [ |
| 245 | "Fix 2 typos in README.md", |
| 246 | "Update section 3", |
| 247 | "Use Python 3", |
| 248 | ] { |
| 249 | let ex = analyze(g); |
| 250 | assert!( |
| 251 | ex.uncertain.is_empty() && ex.requirements.is_empty(), |
| 252 | "{g:?}" |
| 253 | ); |
| 254 | } |
| 255 | } |