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 / support / compose_harness.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 | //! Test harness that drives the compose pipeline through its interface, |
| 17 | //! `ComposeBridge::run_task`, with scripted proposers. |
| 18 | //! |
| 19 | //! The bridge hands every model call to a proposer: the edit proposer for an |
| 20 | //! edit of an existing file, the document proposer for a new document. Both |
| 21 | //! are scripted here and both record what they were handed (the prompt, the |
| 22 | //! time limit, which of the two was asked), so a test observes exactly what |
| 23 | //! the model would see and the report the caller gets back. |
| 24 | //! |
| 25 | //! A goal the bridge refuses before any model call (a bad destination, a |
| 26 | //! workspace escape) comes back as `Err` and needs no composition library. |
| 27 | //! Everything else needs the real librx_compose.a: mark those tests |
| 28 | //! `#[cfg_attr(not(compose_linked), ignore = "needs librx_compose.a: stub build")]`. |
| 29 | //! |
| 30 | //! Every test that opens a home takes `home_guard::home_slot()` first; include |
| 31 | //! both files: |
| 32 | //! `#[path = "support/home_guard.rs"] mod home_guard;` |
| 33 | //! `#[path = "support/compose_harness.rs"] mod compose_harness;` |
| 34 | #![allow(dead_code)] |
| 35 | |
| 36 | use aien_runtime::control::{ComposeTaskReport, ControlResponse}; |
| 37 | use aien_runtime::{ComposeBridge, ComposeProposer, Generation}; |
| 38 | use parking_lot::Mutex; |
| 39 | use std::path::{Path, PathBuf}; |
| 40 | use std::sync::Arc; |
| 41 | use std::time::Duration; |
| 42 | |
| 43 | /// Which proposer the bridge asked: the edit proposer (an existing file is |
| 44 | /// edited) or the document proposer (a new document is written). |
| 45 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 46 | pub enum Asked { |
| 47 | Edit, |
| 48 | Document, |
| 49 | } |
| 50 | |
| 51 | /// One model call the bridge made. |
| 52 | #[derive(Debug, Clone)] |
| 53 | pub struct Call { |
| 54 | /// The full prompt handed to the proposer. |
| 55 | pub prompt: String, |
| 56 | /// The time limit handed with it (what is left of the task's budget). |
| 57 | pub limit: Duration, |
| 58 | pub asked: Asked, |
| 59 | } |
| 60 | |
| 61 | /// The scripted model: called with the 1-based call number (counted over both |
| 62 | /// proposers), the prompt and the limit. |
| 63 | pub type Script = Arc<dyn Fn(usize, &str, Duration) -> Result<Generation, String> + Send + Sync>; |
| 64 | |
| 65 | /// A reply the way a model returns one: `tokens` 5, finish reason `eos`. |
| 66 | pub fn reply(text: &str) -> Result<Generation, String> { |
| 67 | Ok(Generation { |
| 68 | text: text.to_string(), |
| 69 | tokens: 5, |
| 70 | finish_reason: Some("eos".into()), |
| 71 | ..Default::default() |
| 72 | }) |
| 73 | } |
| 74 | |
| 75 | /// A script that answers every call with `text`. |
| 76 | pub fn always(text: &str) -> Script { |
| 77 | let t = text.to_string(); |
| 78 | Arc::new(move |_, _, _| reply(&t)) |
| 79 | } |
| 80 | |
| 81 | /// A script that answers call k with `texts[k - 1]` (the last one repeats). |
| 82 | pub fn replies(texts: &[&str]) -> Script { |
| 83 | let v: Vec<String> = texts.iter().map(|s| s.to_string()).collect(); |
| 84 | Arc::new(move |n, _, _| reply(&v[(n - 1).min(v.len() - 1)])) |
| 85 | } |
| 86 | |
| 87 | /// A script whose model never answers (every call fails): the bridge still |
| 88 | /// shows what it would send. |
| 89 | pub fn silent() -> Script { |
| 90 | Arc::new(|_, _, _| Err("harness: no model reply".to_string())) |
| 91 | } |
| 92 | |
| 93 | /// One compose home and one workspace in a temporary directory, with a bridge |
| 94 | /// whose two proposers run `script` and record every call. |
| 95 | pub struct Harness { |
| 96 | pub tmp: tempfile::TempDir, |
| 97 | pub ws: PathBuf, |
| 98 | pub bridge: ComposeBridge, |
| 99 | calls: Arc<Mutex<Vec<Call>>>, |
| 100 | } |
| 101 | |
| 102 | fn recording(script: Script, calls: Arc<Mutex<Vec<Call>>>, asked: Asked) -> ComposeProposer { |
| 103 | Arc::new(move |prompt: &str, limit: Duration| { |
| 104 | let n = { |
| 105 | let mut c = calls.lock(); |
| 106 | c.push(Call { |
| 107 | prompt: prompt.to_string(), |
| 108 | limit, |
| 109 | asked, |
| 110 | }); |
| 111 | c.len() |
| 112 | }; |
| 113 | script(n, prompt, limit) |
| 114 | }) |
| 115 | } |
| 116 | |
| 117 | impl Harness { |
| 118 | pub fn new(script: Script) -> Self { |
| 119 | let tmp = tempfile::tempdir().unwrap(); |
| 120 | let ws = tmp.path().join("ws"); |
| 121 | std::fs::create_dir_all(&ws).unwrap(); |
| 122 | let ws = std::fs::canonicalize(ws).unwrap(); |
| 123 | let calls: Arc<Mutex<Vec<Call>>> = Arc::default(); |
| 124 | let bridge = ComposeBridge::new( |
| 125 | tmp.path().join("compose"), |
| 126 | recording(script.clone(), calls.clone(), Asked::Edit), |
| 127 | "test:harness", |
| 128 | ) |
| 129 | .with_doc_proposer(recording(script, calls.clone(), Asked::Document)); |
| 130 | Self { |
| 131 | tmp, |
| 132 | ws, |
| 133 | bridge, |
| 134 | calls, |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | /// Write `content` to `rel` inside the workspace (parents created). |
| 139 | pub fn write(&self, rel: &str, content: &str) -> &Self { |
| 140 | let p = self.ws.join(rel); |
| 141 | std::fs::create_dir_all(p.parent().unwrap()).unwrap(); |
| 142 | std::fs::write(p, content).unwrap(); |
| 143 | self |
| 144 | } |
| 145 | |
| 146 | pub fn ws(&self) -> &Path { |
| 147 | &self.ws |
| 148 | } |
| 149 | |
| 150 | /// Run one compose task on this workspace. `Err` is the bridge's refusal text. |
| 151 | pub fn run(&self, goal: &str) -> Result<ComposeTaskReport, String> { |
| 152 | self.run_in(goal, &self.ws) |
| 153 | } |
| 154 | |
| 155 | /// `run` on another workspace directory. |
| 156 | pub fn run_in(&self, goal: &str, ws: &Path) -> Result<ComposeTaskReport, String> { |
| 157 | match self.bridge.run_task(goal, ws.to_str().unwrap()) { |
| 158 | ControlResponse::ComposeTaskResult(r) => Ok(*r), |
| 159 | ControlResponse::Error(e) => Err(e), |
| 160 | other => panic!("unexpected response: {other:?}"), |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | /// Every call so far, in order. |
| 165 | pub fn calls(&self) -> Vec<Call> { |
| 166 | self.calls.lock().clone() |
| 167 | } |
| 168 | |
| 169 | /// Forget the calls so far (the next run starts a fresh list). |
| 170 | pub fn clear(&self) { |
| 171 | self.calls.lock().clear(); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | /// What the bridge sends the model first for `goal` in `ws` (a fresh home, a |
| 176 | /// silent model): the first call, or the refusal text when the bridge refuses |
| 177 | /// the goal before any model call. Also returns the run's report. |
| 178 | pub fn first_call(goal: &str, ws: &Path) -> Result<(Call, ComposeTaskReport), String> { |
| 179 | let h = Harness::new(silent()); |
| 180 | let r = h.run_in(goal, ws)?; |
| 181 | let c = h.calls(); |
| 182 | let first = c |
| 183 | .first() |
| 184 | .cloned() |
| 185 | .unwrap_or_else(|| panic!("the bridge made no model call: {r:?}")); |
| 186 | Ok((first, r)) |
| 187 | } |