Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aien-sovereign-core-mv/blob/main/crates/aien-omegese-constraint/tests/mask_steps.rs # 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 Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aien-sovereign-core-mv / crates / aien-omegese-constraint / tests / mask_steps.rs` 194 lines ยท 8211 bytes Raw Blame History 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 mask step by step, without a model: a driver writes a known program through the 17 //! constraint, and at every masked step every allowed token is checked against an exact oracle 18 //! (soundness), not only the token the driver takes. 19 20 mod common; 21 22 use std::sync::Arc; 23 24 use aien_inference_abi::generation::{Constraint, StepRule}; 25 use aien_omegese_constraint::{Answer, Framing, Item, ItemOracle, OmegeseConstraint, TokenTable}; 26 use common::*; 27 28 const TARGET: &str = "fn f a uint b uint to uint\nret add a b\nend\n"; 29 30 fn framing(fence: bool, header: bool) -> Framing { 31 Framing { 32 open_fence: fence.then(|| "```\n".into()), 33 header: header.then(sig_f), 34 mask_body: true, 35 close_fence: "```".into(), 36 stop_token: STOP, 37 } 38 } 39 40 fn offers_eof(check: &mut ExactText, p: &[u8]) -> bool { 41 matches!(check.next(p).unwrap(), Answer::Items(i) if i.contains(&Item::Eof)) 42 } 43 44 /// Writes `text` through `k`, the longest allowed token that continues it at each step (the 45 /// closing fence or the stop token at its end). At every `Allow` step every allowed token is 46 /// checked: a body token must leave a prefix `check` does not refuse, and a token that ends the 47 /// program (a fence marker, or the stop token) needs eof offered. Returns the tokens (stop 48 /// included) and how many allowed tokens were checked. 49 fn drive( 50 table: &TokenTable, 51 k: &mut OmegeseConstraint, 52 text: &str, 53 check: &mut ExactText, 54 ) -> (Vec, usize) { 55 let mut gen: Vec = Vec::new(); 56 let mut checked = 0; 57 loop { 58 let rule = k.rule(&[], &gen).unwrap_or_else(|e| panic!("{e}")); 59 let prog = k.program().to_vec(); 60 assert!( 61 text.as_bytes().starts_with(&prog), 62 "the program left the text" 63 ); 64 let rest = &text.as_bytes()[prog.len()..]; 65 match rule { 66 StepRule::Force(id) => { 67 gen.push(id); 68 if id == STOP { 69 break; 70 } 71 } 72 StepRule::Allow(set) => { 73 for &id in &set { 74 checked += 1; 75 match table.text(id) { 76 None => assert!(offers_eof(check, &prog), "stop without eof"), 77 Some(t) if t.contains(&b'`') => { 78 assert!(offers_eof(check, &prog), "fence {t:?} without eof") 79 } 80 Some(t) => { 81 let mut p = prog.clone(); 82 p.extend_from_slice(t); 83 assert!( 84 matches!(check.next(&p).unwrap(), Answer::Items(_)), 85 "token {:?} allowed after {:?} leads nowhere", 86 String::from_utf8_lossy(t), 87 String::from_utf8_lossy(&prog) 88 ); 89 } 90 } 91 } 92 let pick = if rest.is_empty() { 93 set.iter() 94 .copied() 95 .find(|&id| table.text(id).is_none_or(|t| t.contains(&b'`'))) 96 .expect("eof offers the closing") 97 } else { 98 set.iter() 99 .copied() 100 .filter(|&id| table.text(id).is_some_and(|t| rest.starts_with(t))) 101 .max_by_key(|&id| table.text(id).unwrap().len()) 102 .unwrap_or_else(|| panic!("no allowed token writes {rest:?}")) 103 }; 104 gen.push(pick); 105 if pick == STOP { 106 break; 107 } 108 } 109 StepRule::Finish => break, 110 StepRule::Any => panic!("Any inside a masked body"), 111 } 112 } 113 (gen, checked) 114 } 115 116 /// Every token the mask allows keeps the program a prefix of the target, in every framing, for a 117 /// target that ends on a newline and one that does not (eof right after `end`). 118 #[test] 119 fn every_allowed_token_keeps_the_target_reachable() { 120 let table = Arc::new(synthetic_table()); 121 let mut total = 0; 122 for target in [TARGET, TARGET.trim_end()] { 123 for (fence, header) in [(true, true), (false, true), (true, false), (false, false)] { 124 let mut k = OmegeseConstraint::new( 125 table.clone(), 126 ExactText::new(target), 127 framing(fence, header), 128 ) 129 .unwrap(); 130 let mut check = ExactText::new(target); 131 let (gen, checked) = drive(&table, &mut k, target, &mut check); 132 total += checked; 133 assert_eq!(k.program(), target.as_bytes()); 134 assert_eq!(*gen.last().unwrap(), STOP); 135 let reply = table.decode(&gen[..gen.len() - 1]).unwrap(); 136 let want = if fence { 137 let nl = if target.ends_with('\n') { "" } else { "\n" }; 138 format!("```\n{target}{nl}```") 139 } else { 140 target.to_string() 141 }; 142 assert_eq!(String::from_utf8(reply).unwrap(), want, "{fence} {header}"); 143 // Without a fence, after `end\n` the stop token is one choice among others (more blank 144 // space may follow): the constraint cannot tell a consumed stop from a cut by length 145 // after another token, so it claims the reply finished only when the stop was forced 146 // (after a bare `end` nothing else may follow). 147 let forced_stop = !target.ends_with('\n'); 148 assert_eq!(k.finished(), fence || forced_stop, "{fence} {header}"); 149 } 150 } 151 assert!(total > 100, "{total} tokens checked"); 152 } 153 154 /// With no token that both starts the closing text and marks it, eof forces the closing: a 155 /// newline (the program ends on `end`), the fence one backtick at a time, then the stop token. 156 #[test] 157 fn the_closing_is_forced_when_no_token_marks_it() { 158 let mut texts = synthetic_texts(); 159 for t in texts.iter_mut() { 160 if t.as_deref() == Some(b"```") || t.as_deref() == Some(b"\n```") { 161 *t = Some(b"zz".to_vec()); 162 } 163 } 164 let table = Arc::new(TokenTable::from_texts(texts)); 165 let target = TARGET.trim_end(); 166 let mut k = 167 OmegeseConstraint::new(table.clone(), ExactText::new(target), framing(true, true)).unwrap(); 168 let mut check = ExactText::new(target); 169 let (gen, _) = drive(&table, &mut k, target, &mut check); 170 let tail: Vec = ["\n", "`", "`", "`"] 171 .iter() 172 .map(|s| table.encode(s).unwrap()[0]) 173 .chain([STOP]) 174 .collect(); 175 assert_eq!(&gen[gen.len() - 5..], &tail[..]); 176 let reply = table.decode(&gen[..gen.len() - 1]).unwrap(); 177 assert_eq!(reply, format!("```\n{target}\n```").into_bytes()); 178 } 179 180 /// A header the oracle refuses is an error when the constraint is built, before anything is 181 /// written; with the mask off the header is not asked about. 182 #[test] 183 fn a_header_the_oracle_refuses_fails_at_construction() { 184 let table = Arc::new(synthetic_table()); 185 let other = "fn g a uint to uint\nret a\nend\n"; 186 let e = OmegeseConstraint::new(table.clone(), ExactText::new(other), framing(true, true)) 187 .err() 188 .expect("the header is refused"); 189 assert!(e.contains("refuses the header"), "{e}"); 190 let mut unmasked = framing(true, true); 191 unmasked.mask_body = false; 192 let k = OmegeseConstraint::new(table, ExactText::new(other), unmasked).unwrap(); 193 assert_eq!(k.oracle().calls, 0); 194 }