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 / logical_workspace_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 | //! sc#429: the model-visible prompt names the workspace by a constant logical |
| 17 | //! label, never by its machine-specific absolute path. Two workspaces with the |
| 18 | //! same logical inputs (goal, files, contents) at different absolute paths |
| 19 | //! (different parent folders, depths and names, as a different host, user or |
| 20 | //! job folder would give) must produce byte-identical prompts and, with a |
| 21 | //! tokenizer, identical prompt token ids. The real path stays in the |
| 22 | //! authorization and evidence layers (the canonical workspace is still |
| 23 | //! resolved, and the task id still hashes it). |
| 24 | //! |
| 25 | //! The prompt is the one `ComposeBridge::run_task` hands the model |
| 26 | //! (tests/support/compose_harness.rs), one harness running both workspaces. |
| 27 | #[path = "support/compose_harness.rs"] |
| 28 | mod compose_harness; |
| 29 | #[path = "support/home_guard.rs"] |
| 30 | mod home_guard; |
| 31 | |
| 32 | use compose_harness::{silent, Harness}; |
| 33 | use std::path::Path; |
| 34 | |
| 35 | /// The logical name the prompt gives the workspace. |
| 36 | const WORKSPACE_LABEL: &str = "workspace"; |
| 37 | |
| 38 | /// The first prompt the bridge hands the model for `goal` in `ws`. |
| 39 | fn prompt(h: &Harness, goal: &str, ws: &Path) -> String { |
| 40 | h.clear(); |
| 41 | let r = h |
| 42 | .run_in(goal, ws) |
| 43 | .unwrap_or_else(|e| panic!("{goal}: refused: {e}")); |
| 44 | h.calls() |
| 45 | .first() |
| 46 | .unwrap_or_else(|| panic!("{goal}: no model call: {r:?}")) |
| 47 | .prompt |
| 48 | .clone() |
| 49 | } |
| 50 | |
| 51 | fn fill(ws: &Path) { |
| 52 | std::fs::create_dir_all(ws.join("inbox")).unwrap(); |
| 53 | std::fs::write(ws.join("inbox/tide.txt"), "High tide at 06:40.\n").unwrap(); |
| 54 | std::fs::write(ws.join("inbox/berth.txt"), "Berth 4 is free.\n").unwrap(); |
| 55 | std::fs::write(ws.join("inbox/crane.txt"), "Crane service at 09:00.\n").unwrap(); |
| 56 | std::fs::write(ws.join("report.md"), "# Harbour report\n\n- morning\n").unwrap(); |
| 57 | } |
| 58 | |
| 59 | /// Two equal workspaces at deliberately different absolute paths. |
| 60 | fn pair() -> ( |
| 61 | tempfile::TempDir, |
| 62 | tempfile::TempDir, |
| 63 | std::path::PathBuf, |
| 64 | std::path::PathBuf, |
| 65 | ) { |
| 66 | let a_root = tempfile::tempdir().unwrap(); |
| 67 | let b_root = tempfile::tempdir().unwrap(); |
| 68 | let a = a_root |
| 69 | .path() |
| 70 | .join("jobs") |
| 71 | .join("host-alpha") |
| 72 | .join("ws-contract"); |
| 73 | let b = b_root |
| 74 | .path() |
| 75 | .join("home") |
| 76 | .join("other-user") |
| 77 | .join("deeper") |
| 78 | .join("run") |
| 79 | .join("ws-contract-2"); |
| 80 | fill(&a); |
| 81 | fill(&b); |
| 82 | (a_root, b_root, a, b) |
| 83 | } |
| 84 | |
| 85 | const GOALS: &[&str] = &[ |
| 86 | "Create the file NOTES.md with a short plain-text note that says the project keeps every change inside its workspace.", |
| 87 | "Write the file summary.md from the files tide.txt, berth.txt and crane.txt in the inbox folder.", |
| 88 | "Add the line \"- evening\" under the \"# Harbour report\" heading in report.md.", |
| 89 | ]; |
| 90 | |
| 91 | #[test] |
| 92 | #[cfg_attr(not(compose_linked), ignore = "needs librx_compose.a: stub build")] |
| 93 | fn prompt_carries_the_label_and_never_the_path() { |
| 94 | let _home = home_guard::home_slot(); |
| 95 | let h = Harness::new(silent()); |
| 96 | let (_ka, _kb, a, b) = pair(); |
| 97 | let a = std::fs::canonicalize(&a).unwrap(); |
| 98 | let b = std::fs::canonicalize(&b).unwrap(); |
| 99 | assert_ne!(a, b); |
| 100 | for goal in GOALS { |
| 101 | let pa = prompt(&h, goal, &a); |
| 102 | let pb = prompt(&h, goal, &b); |
| 103 | assert_eq!(pa, pb, "prompt differs between workspaces for goal {goal}"); |
| 104 | assert!( |
| 105 | pa.starts_with(&format!( |
| 106 | "Goal: {goal}\nAuthorized workspace: {WORKSPACE_LABEL}\n" |
| 107 | )), |
| 108 | "label missing in {pa}" |
| 109 | ); |
| 110 | for p in [&a, &b] { |
| 111 | let s = p.display().to_string(); |
| 112 | assert!(!pa.contains(&s), "absolute path {s} leaked into the prompt"); |
| 113 | // not even the leaf folder name |
| 114 | let leaf = p.file_name().unwrap().to_str().unwrap(); |
| 115 | assert!( |
| 116 | !pa.contains(leaf), |
| 117 | "folder name {leaf} leaked into the prompt" |
| 118 | ); |
| 119 | } |
| 120 | } |
| 121 | // The whole prompt of a plain new-document goal (the base template plus |
| 122 | // its destination line) carries no path separator at all. |
| 123 | let base = prompt(&h, GOALS[0], &a); |
| 124 | assert!( |
| 125 | !base.contains('/'), |
| 126 | "no path separator in the base prompt: {base}" |
| 127 | ); |
| 128 | } |
| 129 | |
| 130 | /// With a tokenizer (`AIEN_TOKENIZER_PATH`), the token ids of the rendered |
| 131 | /// prompt are equal across the two paths, for every chat template the product |
| 132 | /// serves. |
| 133 | #[test] |
| 134 | #[cfg_attr(not(compose_linked), ignore = "needs librx_compose.a: stub build")] |
| 135 | fn prompt_token_ids_equal_across_paths() { |
| 136 | let _home = home_guard::home_slot(); |
| 137 | let Ok(path) = std::env::var("AIEN_TOKENIZER_PATH") else { |
| 138 | eprintln!("AIEN_TOKENIZER_PATH not set: token-id check skipped"); |
| 139 | return; |
| 140 | }; |
| 141 | use aien_inference_abi::TinyLlamaTokenizer; |
| 142 | use aien_runtime::control::{format_chat, ChatTurn}; |
| 143 | let tok = TinyLlamaTokenizer::from_file(path).expect("tokenizer"); |
| 144 | let h = Harness::new(silent()); |
| 145 | let (_ka, _kb, a, b) = pair(); |
| 146 | let a = std::fs::canonicalize(&a).unwrap(); |
| 147 | let b = std::fs::canonicalize(&b).unwrap(); |
| 148 | for goal in GOALS { |
| 149 | let pa = prompt(&h, goal, &a); |
| 150 | let pb = prompt(&h, goal, &b); |
| 151 | let ids = |p: &str| { |
| 152 | let turn = ChatTurn { |
| 153 | role: "user".into(), |
| 154 | content: p.into(), |
| 155 | }; |
| 156 | let text = format_chat(tok.template(), std::slice::from_ref(&turn)).expect("chat"); |
| 157 | tok.encode(&text).expect("encode") |
| 158 | }; |
| 159 | let ia = ids(&pa); |
| 160 | let ib = ids(&pb); |
| 161 | assert_eq!(ia, ib, "token ids differ between paths for goal {goal}"); |
| 162 | eprintln!( |
| 163 | "goal {:.40}...: {} prompt tokens, equal on both paths", |
| 164 | goal, |
| 165 | ia.len() |
| 166 | ); |
| 167 | } |
| 168 | } |