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-omegese-constraint / tests / common / mod.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 | //! Shared by the host tests: the 64-token synthetic table, an exact one-program oracle, and the |
| 17 | //! real binary's path from the environment. |
| 18 | #![allow(dead_code)] |
| 19 | |
| 20 | use std::path::PathBuf; |
| 21 | use std::process::Command; |
| 22 | |
| 23 | use aien_omegese_constraint::constraint::canonical_seps; |
| 24 | use aien_omegese_constraint::word::{current_word, is_sep}; |
| 25 | use aien_omegese_constraint::{Answer, Item, ItemOracle, Signature, TokenTable}; |
| 26 | |
| 27 | /// The synthetic table's stop token (a special token, no text). |
| 28 | pub const STOP: u32 = 63; |
| 29 | |
| 30 | /// 64 tokens for the 64-token `EchoChainModel`: every single byte Omegese uses (so a mask is |
| 31 | /// never empty), the fence, and multi-item tokens with one boundary (` add`, `end\n`, `a b`, |
| 32 | /// `)\n`, and ` \n`, a blank run that reads as its newline) and with two (` a `, `(b)`). |
| 33 | pub fn synthetic_texts() -> Vec<Option<Vec<u8>>> { |
| 34 | let mut t: Vec<Option<Vec<u8>>> = Vec::new(); |
| 35 | for c in b'a'..=b'z' { |
| 36 | t.push(Some(vec![c])); |
| 37 | } |
| 38 | for c in b'0'..=b'9' { |
| 39 | t.push(Some(vec![c])); |
| 40 | } |
| 41 | for s in [ |
| 42 | "_", "-", "=", " ", "\n", "(", ")", "`", "```", "end", "ret", "uint", " uint", " add", |
| 43 | " a", " b", "\n\n", ")\n", " (", "end\n", "a b", " a ", "(b)", "if", " \n", "\n```", "fn", |
| 44 | ] { |
| 45 | t.push(Some(s.as_bytes().to_vec())); |
| 46 | } |
| 47 | t.push(None); |
| 48 | assert_eq!(t.len(), 64); |
| 49 | assert_eq!(t[STOP as usize], None); |
| 50 | t |
| 51 | } |
| 52 | |
| 53 | pub fn synthetic_table() -> TokenTable { |
| 54 | TokenTable::from_texts(synthetic_texts()) |
| 55 | } |
| 56 | |
| 57 | pub fn sig_f() -> Signature { |
| 58 | Signature::from_requirement("entry f\nparams u32 u32\nret u32\n").unwrap() |
| 59 | } |
| 60 | |
| 61 | /// An exact oracle for the one-program language {`target`} (prefix-closed; `target` has single |
| 62 | /// blanks), blank runs read as Omegese reads them: what a writer may add is exactly the next |
| 63 | /// piece of `target`, or more blank space after blank space. |
| 64 | pub struct ExactText { |
| 65 | pub target: Vec<u8>, |
| 66 | pub calls: usize, |
| 67 | } |
| 68 | |
| 69 | impl ExactText { |
| 70 | pub fn new(target: &str) -> Self { |
| 71 | Self { |
| 72 | target: target.as_bytes().to_vec(), |
| 73 | calls: 0, |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | impl ItemOracle for ExactText { |
| 79 | fn next(&mut self, raw: &[u8]) -> Result<Answer, String> { |
| 80 | self.calls += 1; |
| 81 | // Blank runs read in canonical form, as Omegese reads them (the mask relies on it). |
| 82 | let p = &normalize(raw); |
| 83 | let t = &self.target; |
| 84 | if !t.starts_with(p) { |
| 85 | return Ok(Answer::Refused( |
| 86 | "REFUSED omegese: line 1: not the target".into(), |
| 87 | )); |
| 88 | } |
| 89 | let u = current_word(p); |
| 90 | let mut items = Vec::new(); |
| 91 | let word_end = t[p.len()..] |
| 92 | .iter() |
| 93 | .position(|&b| is_sep(b)) |
| 94 | .map_or(t.len(), |k| p.len() + k); |
| 95 | let start = p.len() - u.len(); |
| 96 | if word_end > start { |
| 97 | items.push(Item::Word(t[start..word_end].to_vec())); |
| 98 | } |
| 99 | if p.len() == t.len() { |
| 100 | items.push(Item::Eof); |
| 101 | } else if word_end == p.len() { |
| 102 | items.push(match t[p.len()] { |
| 103 | b' ' | b'\t' => Item::Space, |
| 104 | b'\n' => Item::Newline, |
| 105 | b'(' => Item::Open, |
| 106 | _ => Item::Close, |
| 107 | }); |
| 108 | } |
| 109 | // More blank space after blank space reads the same (the target has no two blanks in |
| 110 | // a row, so these are new items). |
| 111 | match p.last() { |
| 112 | Some(b' ') => items.push(Item::Space), |
| 113 | Some(b'\n') => items.extend([Item::Space, Item::Newline]), |
| 114 | _ => {} |
| 115 | } |
| 116 | Ok(Answer::Items(items)) |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | /// Every separator run in `canonical_seps` form. |
| 121 | pub fn normalize(p: &[u8]) -> Vec<u8> { |
| 122 | let mut out = Vec::new(); |
| 123 | let mut i = 0; |
| 124 | while i < p.len() { |
| 125 | if is_sep(p[i]) { |
| 126 | let j = p[i..] |
| 127 | .iter() |
| 128 | .position(|&b| !is_sep(b)) |
| 129 | .map_or(p.len(), |k| i + k); |
| 130 | out.extend(canonical_seps(&p[i..j])); |
| 131 | i = j; |
| 132 | } else { |
| 133 | out.push(p[i]); |
| 134 | i += 1; |
| 135 | } |
| 136 | } |
| 137 | out |
| 138 | } |
| 139 | |
| 140 | /// `OMEGESE_BIN`: the `omegese` binary built from omega main (`make omegese`). The tests that need |
| 141 | /// it are `#[ignore]` and fail, never skip, when it is missing. |
| 142 | pub fn omegese_bin() -> PathBuf { |
| 143 | let p = std::env::var_os("OMEGESE_BIN") |
| 144 | .map(PathBuf::from) |
| 145 | .expect("set OMEGESE_BIN to the omegese binary built from omega main"); |
| 146 | assert!(p.is_file(), "OMEGESE_BIN {} is not a file", p.display()); |
| 147 | p |
| 148 | } |
| 149 | |
| 150 | fn scratch(name: &str, body: &[u8]) -> PathBuf { |
| 151 | let dir = std::env::temp_dir().join(format!("omegese-constraint-{}", std::process::id())); |
| 152 | std::fs::create_dir_all(&dir).unwrap(); |
| 153 | let p = dir.join(name); |
| 154 | std::fs::write(&p, body).unwrap(); |
| 155 | p |
| 156 | } |
| 157 | |
| 158 | /// `omegese emit` on `program`: `Ok(())` accepted, `Err(its line)` refused. |
| 159 | pub fn emit(bin: &PathBuf, program: &[u8]) -> Result<(), String> { |
| 160 | let src = scratch("emit.omegese", program); |
| 161 | let out = src.with_extension("json"); |
| 162 | let o = Command::new(bin) |
| 163 | .arg("emit") |
| 164 | .arg(&src) |
| 165 | .arg(&out) |
| 166 | .output() |
| 167 | .unwrap(); |
| 168 | match o.status.code() { |
| 169 | Some(0) => Ok(()), |
| 170 | _ => Err(String::from_utf8_lossy(&o.stdout).trim().to_string()), |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | /// `omegese complete` on `prefix`: the text that finishes it. |
| 175 | pub fn complete(bin: &PathBuf, prefix: &[u8]) -> Result<Vec<u8>, String> { |
| 176 | let src = scratch("complete.omegese", prefix); |
| 177 | let o = Command::new(bin) |
| 178 | .arg("complete") |
| 179 | .arg(&src) |
| 180 | .output() |
| 181 | .unwrap(); |
| 182 | match o.status.code() { |
| 183 | Some(0) => Ok(o.stdout), |
| 184 | _ => Err(String::from_utf8_lossy(&o.stdout).trim().to_string()), |
| 185 | } |
| 186 | } |