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_first_heading_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 | //! A goal that asks the document to start with a markdown heading |
| 17 | //! (Requirement::FirstLineHeading). The ALLEN end-to-end demo v2 request was |
| 18 | //! refused as uncertain because no recognizer read this wording (arch#162). |
| 19 | //! Recognizing it must not loosen any other uncertain wording. |
| 20 | #[path = "support/compose_harness.rs"] |
| 21 | mod compose_harness; |
| 22 | #[path = "support/home_guard.rs"] |
| 23 | mod home_guard; |
| 24 | |
| 25 | use aien_runtime::check_file_proposal; |
| 26 | use aien_runtime::control::ComposeTaskReport; |
| 27 | use aien_runtime::requirements::{analyze, refusal_reason, Requirement}; |
| 28 | use compose_harness::Harness; |
| 29 | |
| 30 | use Requirement::*; |
| 31 | |
| 32 | /// The S3 request of docs/campaigns/allen-e2e/DEMO-v2.md, verbatim. |
| 33 | const DEMO_V2_GOAL: &str = "Write garden.md in the workspace, a garden plan. Start with a Markdown heading line that begins with \"# \". Use at most 200 words."; |
| 34 | |
| 35 | /// One compose task through `ComposeBridge::run_task`: the model answers call |
| 36 | /// k with `replies[k - 1]` (the last one repeats). Returns the report and the |
| 37 | /// prompts the model was handed. |
| 38 | fn run(goal: &str, replies: Vec<String>) -> (ComposeTaskReport, Vec<String>) { |
| 39 | let ex = analyze(goal); |
| 40 | assert!(ex.uncertain.is_empty(), "{:?}", ex.uncertain); |
| 41 | let texts: Vec<&str> = replies.iter().map(String::as_str).collect(); |
| 42 | let h = Harness::new(compose_harness::replies(&texts)); |
| 43 | let r = h.run(goal).unwrap(); |
| 44 | let p = h.calls().into_iter().map(|c| c.prompt).collect(); |
| 45 | (r, p) |
| 46 | } |
| 47 | |
| 48 | const NO_HEADING: &str = |
| 49 | "filename: garden.md\nGarden plan\n\nPlant tomatoes in May and water them weekly.\n"; |
| 50 | const GOOD: &str = |
| 51 | "filename: garden.md\n# Garden plan\n\nPlant tomatoes in May and water them weekly.\n"; |
| 52 | |
| 53 | #[test] |
| 54 | fn demo_v2_goal_is_fully_recognized() { |
| 55 | let ex = analyze(DEMO_V2_GOAL); |
| 56 | assert!(ex.uncertain.is_empty(), "{:?}", ex.uncertain); |
| 57 | assert_eq!( |
| 58 | ex.requirements, |
| 59 | [MaxWords(200), FirstLineHeading { level: Some(1) }] |
| 60 | ); |
| 61 | // The S8 request differs only in the file name. |
| 62 | let s8 = DEMO_V2_GOAL.replace("garden.md", "garden-b.md"); |
| 63 | assert_eq!(analyze(&s8), ex); |
| 64 | } |
| 65 | |
| 66 | #[test] |
| 67 | fn first_heading_phrasings() { |
| 68 | for (g, want) in [ |
| 69 | ( |
| 70 | "Start with a Markdown heading line that begins with \"# \".", |
| 71 | Some(1), |
| 72 | ), |
| 73 | ( |
| 74 | "start with a markdown heading line that starts with \"## \"", |
| 75 | Some(2), |
| 76 | ), |
| 77 | ("Begin with a heading.", None), |
| 78 | ("Start with a Markdown heading.", None), |
| 79 | ("It must start with a level-two heading.", Some(2)), |
| 80 | ("Open with a markdown heading line.", None), |
| 81 | ( |
| 82 | "Start with a heading that begins with \u{201C}# \u{201D}.", |
| 83 | Some(1), |
| 84 | ), |
| 85 | ] { |
| 86 | let ex = analyze(g); |
| 87 | assert!(ex.uncertain.is_empty(), "{g}: {:?}", ex.uncertain); |
| 88 | assert_eq!(ex.requirements, [FirstLineHeading { level: want }], "{g}"); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | #[test] |
| 93 | fn near_misses_stay_uncertain() { |
| 94 | for g in [ |
| 95 | // negated: never inverted |
| 96 | "Don't start with a Markdown heading.", |
| 97 | // a marker that is not a heading marker on its own |
| 98 | "Start with a Markdown heading line that begins with \"#\".", |
| 99 | // the level adjective and the marker disagree |
| 100 | "Start with a level-one heading line that begins with \"## \".", |
| 101 | // seven hashes is not a heading |
| 102 | "Start with a heading line that begins with \"####### \".", |
| 103 | // emphasis around the wording |
| 104 | "Start with a **Markdown heading**.", |
| 105 | "Start with a **heading**.", |
| 106 | // another word before "heading" |
| 107 | "Begin with a short heading.", |
| 108 | // a qualifier after the heading |
| 109 | "Start with a Markdown heading about tomatoes.", |
| 110 | "Start with a Markdown heading line that begins with \"# \" in bold.", |
| 111 | // no marker after "begins with" |
| 112 | "Start with a Markdown heading line that begins with a hash.", |
| 113 | // a sibling constraint in the same sentence is never swallowed (R319 MUST 1) |
| 114 | "Begin with a markdown heading and keep it brief", |
| 115 | "Begin with a markdown heading and lowercase only", |
| 116 | "Begin with a markdown heading and end with a signature", |
| 117 | "Begin with a markdown heading, then a table", |
| 118 | "Begin with a markdown heading and a few sections", |
| 119 | "Open with a level-2 heading line and a short list", |
| 120 | "Open with a markdown heading line, and keep it short.", |
| 121 | "Start with a heading line that begins with \"# \" and at most 5 items.", |
| 122 | "Start with a heading, ####### markdown.", |
| 123 | // other heading cues the safety net already refused |
| 124 | "The page needs a Summary heading.", |
| 125 | "Include headings for setup and use.", |
| 126 | ] { |
| 127 | let ex = analyze(g); |
| 128 | assert!(!ex.uncertain.is_empty(), "{g}: should be uncertain"); |
| 129 | assert!( |
| 130 | !ex.requirements |
| 131 | .iter() |
| 132 | .any(|r| matches!(r, FirstLineHeading { .. })), |
| 133 | "{g}: {:?}", |
| 134 | ex.requirements |
| 135 | ); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | #[test] |
| 140 | fn the_first_line_is_checked() { |
| 141 | let one = FirstLineHeading { level: Some(1) }; |
| 142 | let any = FirstLineHeading { level: None }; |
| 143 | for (doc, ok1, ok_any) in [ |
| 144 | ("# Garden plan\nbody\n", true, true), |
| 145 | ("# Garden plan\r\nbody\r\n", true, true), |
| 146 | ("# Garden plan", true, true), |
| 147 | ("## Garden plan\n", false, true), |
| 148 | ("###### Six\n", false, true), |
| 149 | ("####### Seven\n", false, false), |
| 150 | ("Garden plan\n# Later\n", false, false), |
| 151 | ("\n# Garden plan\n", false, false), |
| 152 | (" # Indented\n", false, false), |
| 153 | ("#Garden\n", false, false), |
| 154 | ("# \nbody\n", false, false), |
| 155 | ("#\n", false, false), |
| 156 | ("", false, false), |
| 157 | ("\u{feff}# Garden plan\n", false, false), |
| 158 | ] { |
| 159 | assert_eq!(one.check(doc).is_none(), ok1, "level 1 on {doc:?}"); |
| 160 | assert_eq!(any.check(doc).is_none(), ok_any, "any level on {doc:?}"); |
| 161 | } |
| 162 | let why = one.check("Garden plan\n").unwrap(); |
| 163 | assert!(why.contains("first line"), "{why}"); |
| 164 | } |
| 165 | |
| 166 | #[test] |
| 167 | #[cfg_attr(not(compose_linked), ignore = "needs librx_compose.a: stub build")] |
| 168 | fn a_reply_without_the_heading_is_refused_then_a_good_one_passes() { |
| 169 | let _home = home_guard::home_slot(); |
| 170 | let ex = analyze(DEMO_V2_GOAL); |
| 171 | let content = check_file_proposal(NO_HEADING).unwrap().content; |
| 172 | let why = refusal_reason(&ex.requirements, &content).unwrap(); |
| 173 | assert!(why.contains("first line"), "{why}"); |
| 174 | let (r, prompts) = run(DEMO_V2_GOAL, vec![NO_HEADING.into(), GOOD.into()]); |
| 175 | assert_eq!(r.proposal.as_deref(), Some(GOOD), "{r:?}"); |
| 176 | let a = &r.proposal_attempts; |
| 177 | assert_eq!(a[0].outcome, "refused"); |
| 178 | assert_eq!(a[1].outcome, "parsed"); |
| 179 | // the retry prompt names what was missed |
| 180 | assert!(prompts[1].contains("first line"), "{}", prompts[1]); |
| 181 | } |
| 182 | |
| 183 | #[test] |
| 184 | #[cfg_attr(not(compose_linked), ignore = "needs librx_compose.a: stub build")] |
| 185 | fn a_reply_that_never_has_the_heading_exhausts_with_nothing_approved() { |
| 186 | let _home = home_guard::home_slot(); |
| 187 | let (r, _) = run(DEMO_V2_GOAL, vec![NO_HEADING.into()]); |
| 188 | assert!( |
| 189 | r.proposal.is_none() && r.uncommitted_proposal.is_none(), |
| 190 | "{r:?}" |
| 191 | ); |
| 192 | assert!(r.proposer_error.unwrap().contains("first line")); |
| 193 | let a = &r.proposal_attempts; |
| 194 | assert_eq!(a.len(), 3); |
| 195 | assert!(a |
| 196 | .iter() |
| 197 | .all(|x| x.outcome == "refused" && x.aegis.is_none())); |
| 198 | } |