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 / compose_budget_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 | //! The bridge's time rules under short injected budgets, driven through |
| 17 | //! `ComposeBridge::run_task` (tests/support/compose_harness.rs). |
| 18 | //! |
| 19 | //! Own test binary: it sets the process-global budget variables. Every test |
| 20 | //! takes the compose-home slot first and sets and clears the variables while |
| 21 | //! holding it; the bridge reads them when it opens its home, which also |
| 22 | //! happens under the slot. |
| 23 | #[path = "support/compose_harness.rs"] |
| 24 | mod compose_harness; |
| 25 | #[path = "support/home_guard.rs"] |
| 26 | mod home_guard; |
| 27 | |
| 28 | use compose_harness::{reply, Asked, Harness, Script}; |
| 29 | use std::sync::Arc; |
| 30 | use std::time::Duration; |
| 31 | |
| 32 | const EDIT_BUDGET_ENV: &str = "AIEN_COMPOSE_EDIT_BUDGET_MS"; |
| 33 | const DOC_BUDGET_ENV: &str = "AIEN_COMPOSE_DOC_BUDGET_MS"; |
| 34 | |
| 35 | /// Sets the budget variables for one bridge and clears them when dropped. |
| 36 | struct Budgets; |
| 37 | |
| 38 | impl Budgets { |
| 39 | fn set(edit_ms: Option<u64>, doc_ms: Option<u64>) -> Self { |
| 40 | for (k, v) in [(EDIT_BUDGET_ENV, edit_ms), (DOC_BUDGET_ENV, doc_ms)] { |
| 41 | match v { |
| 42 | Some(ms) => std::env::set_var(k, ms.to_string()), |
| 43 | None => std::env::remove_var(k), |
| 44 | } |
| 45 | } |
| 46 | Budgets |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | impl Drop for Budgets { |
| 51 | fn drop(&mut self) { |
| 52 | std::env::remove_var(EDIT_BUDGET_ENV); |
| 53 | std::env::remove_var(DOC_BUDGET_ENV); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | /// A harness whose bridge opened its home under `budgets` (the variables are |
| 58 | /// read once, at the first run). |
| 59 | fn harness(budgets: Budgets, script: Script) -> (Budgets, Harness) { |
| 60 | (budgets, Harness::new(script)) |
| 61 | } |
| 62 | |
| 63 | // NEG (corrective_retry_test): the budget rule is unchanged; attempt 2 does |
| 64 | // not start when less than the per-attempt budget is left, feedback or not. |
| 65 | // Document budget 1000 ms: the per-attempt budget is half of it, 500 ms. |
| 66 | #[test] |
| 67 | #[cfg_attr(not(compose_linked), ignore = "needs librx_compose.a: stub build")] |
| 68 | fn budget_rule_is_unchanged() { |
| 69 | let _home = home_guard::home_slot(); |
| 70 | let (_b, h) = harness( |
| 71 | Budgets::set(None, Some(1000)), |
| 72 | Arc::new(|_, _, _| { |
| 73 | std::thread::sleep(Duration::from_millis(600)); |
| 74 | reply("no header") |
| 75 | }), |
| 76 | ); |
| 77 | let r = h.run("write a short note into NOTES.md").unwrap(); |
| 78 | assert_eq!(r.proposal_attempts.len(), 1, "{r:?}"); |
| 79 | assert!( |
| 80 | r.proposer_error |
| 81 | .as_deref() |
| 82 | .unwrap() |
| 83 | .contains("no attempt 2"), |
| 84 | "{r:?}" |
| 85 | ); |
| 86 | } |
| 87 | |
| 88 | // compose_task_test::retry_is_capped_and_recorded, its budget part: attempt 2 |
| 89 | // does not start when less than the per-attempt budget is left. Document |
| 90 | // budget 1000 ms: the per-attempt budget is half of it, 500 ms. |
| 91 | #[test] |
| 92 | #[cfg_attr(not(compose_linked), ignore = "needs librx_compose.a: stub build")] |
| 93 | fn retry_is_capped_and_recorded() { |
| 94 | let _home = home_guard::home_slot(); |
| 95 | let (_b, h) = harness( |
| 96 | Budgets::set(None, Some(1000)), |
| 97 | Arc::new(|_, _, _| { |
| 98 | std::thread::sleep(Duration::from_millis(600)); |
| 99 | reply("Sure!") |
| 100 | }), |
| 101 | ); |
| 102 | let r = h.run("write a short note into NOTES.md").unwrap(); |
| 103 | assert_eq!(h.calls().len(), 1); |
| 104 | assert_eq!(r.proposal_attempts.len(), 1, "{r:?}"); |
| 105 | assert!( |
| 106 | r.proposer_error |
| 107 | .as_deref() |
| 108 | .unwrap() |
| 109 | .contains("no attempt 2"), |
| 110 | "{r:?}" |
| 111 | ); |
| 112 | } |
| 113 | |
| 114 | // compose_task_test::kind_selects_budget, its per-attempt part: an edit keeps |
| 115 | // the measured 12 s attempt budget whatever its task budget; a document needs |
| 116 | // about the shortest measured full document (30 s), capped at half a small |
| 117 | // custom budget. The 30 s cap takes about 32 s of model time to show. |
| 118 | #[test] |
| 119 | #[cfg_attr(not(compose_linked), ignore = "needs librx_compose.a: stub build")] |
| 120 | fn kind_selects_budget() { |
| 121 | let _home = home_guard::home_slot(); |
| 122 | let refused = |sleep_ms: u64| -> Script { |
| 123 | Arc::new(move |_, _, _| { |
| 124 | std::thread::sleep(Duration::from_millis(sleep_ms)); |
| 125 | reply("Sure!") |
| 126 | }) |
| 127 | }; |
| 128 | // edit, budget 1 s: 12 000 ms per attempt (not half the budget), so no attempt 2 |
| 129 | { |
| 130 | let (_b, h) = harness(Budgets::set(Some(1000), None), refused(0)); |
| 131 | h.write("notes.md", "# notes\n"); |
| 132 | let r = h.run("add a line to notes.md").unwrap(); |
| 133 | assert_eq!(h.calls().len(), 1, "{r:?}"); |
| 134 | assert_eq!(h.calls()[0].asked, Asked::Edit); |
| 135 | let why = r.proposer_error.unwrap(); |
| 136 | assert!( |
| 137 | why.contains("no attempt 2") && why.contains("< 12000 ms per-attempt budget"), |
| 138 | "{why}" |
| 139 | ); |
| 140 | } |
| 141 | // document, budget 1 s: half of it, 500 ms |
| 142 | { |
| 143 | let (_b, h) = harness(Budgets::set(None, Some(1000)), refused(600)); |
| 144 | let r = h.run("write a new file faq.md").unwrap(); |
| 145 | assert_eq!(h.calls().len(), 1, "{r:?}"); |
| 146 | assert_eq!(h.calls()[0].asked, Asked::Document); |
| 147 | let why = r.proposer_error.unwrap(); |
| 148 | assert!( |
| 149 | why.contains("no attempt 2") && why.contains("< 500 ms per-attempt budget"), |
| 150 | "{why}" |
| 151 | ); |
| 152 | } |
| 153 | // document, budget 61 s: 30 000 ms (the cap), not half the budget (30 500 ms) |
| 154 | { |
| 155 | let (_b, h) = harness(Budgets::set(None, Some(61_000)), refused(31_500)); |
| 156 | let r = h.run("write a new file faq.md").unwrap(); |
| 157 | assert_eq!(h.calls().len(), 1, "{r:?}"); |
| 158 | assert_eq!(h.calls()[0].asked, Asked::Document); |
| 159 | let why = r.proposer_error.unwrap(); |
| 160 | assert!( |
| 161 | why.contains("no attempt 2") && why.contains("< 30000 ms per-attempt budget"), |
| 162 | "{why}" |
| 163 | ); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | // requirements_test::deadline_is_shared_across_attempts. No tight margins: |
| 168 | // every assertion is a one-sided bound that sleeping longer than planned (a |
| 169 | // loaded machine) can only make easier to meet, except that attempt 1 must end |
| 170 | // within 1 000 ms. Document budget 2000 ms: the per-attempt budget is 1000 ms. |
| 171 | #[test] |
| 172 | #[cfg_attr(not(compose_linked), ignore = "needs librx_compose.a: stub build")] |
| 173 | fn deadline_is_shared_across_attempts() { |
| 174 | let _home = home_guard::home_slot(); |
| 175 | let doc1 = "filename: DOC.md\nline 0\n"; |
| 176 | let (_b, h) = harness( |
| 177 | Budgets::set(None, Some(2000)), |
| 178 | Arc::new(move |_, _, _| { |
| 179 | std::thread::sleep(Duration::from_millis(600)); |
| 180 | reply(doc1) |
| 181 | }), |
| 182 | ); |
| 183 | let r = h.run("write DOC.md in at least 20 lines").unwrap(); |
| 184 | assert!(r.proposal.is_none(), "{r:?}"); |
| 185 | let seen: Vec<Duration> = h.calls().iter().map(|c| c.limit).collect(); |
| 186 | let a = &r.proposal_attempts; |
| 187 | assert_eq!(seen.len(), a.len()); |
| 188 | // A per-attempt reset would allow all 3 attempts. One shared deadline |
| 189 | // stops attempt k > 1 unless 1000 ms are left, and every earlier attempt |
| 190 | // took at least 600 ms, so k - 1 <= (2000 - 1000) / 600: at most 2. |
| 191 | assert_eq!(a.len(), 2, "attempts {a:?}"); |
| 192 | // Each attempt's limit is what is left of ONE budget: attempt k starts |
| 193 | // after k - 1 sleeps of at least 600 ms, so it is at least that far |
| 194 | // below the first limit. |
| 195 | for k in 1..seen.len() { |
| 196 | assert!( |
| 197 | seen[k] + Duration::from_millis(590 * k as u64) <= seen[0], |
| 198 | "attempt {} limit {:?} vs first {:?}", |
| 199 | k + 1, |
| 200 | seen[k], |
| 201 | seen[0] |
| 202 | ); |
| 203 | } |
| 204 | assert!(seen[0] <= Duration::from_millis(2000)); |
| 205 | assert!(a.iter().all(|x| x.outcome == "refused")); |
| 206 | assert!(r.proposer_error.unwrap().contains("per-attempt budget")); |
| 207 | } |