omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / research / atemporal / at1 / oracle / src / main.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 | //! AT-1 reference oracle, outer layer: the only place that touches files, the process |
| 17 | //! table and the wall clock. Everything that computes lives in src/at1/. |
| 18 | //! |
| 19 | //! Usage: |
| 20 | //! at1-oracle emit <case-file> [-o <out-file>] write an AT1_RESULT_V1 oracle record |
| 21 | //! at1-oracle fixtures <dir> write the calibration fixtures |
| 22 | //! at1-oracle check <case-file> validate only; print the ids |
| 23 | //! A refused case prints exactly `AT1_CASE_REFUSED <code>` on stderr and exits 2. |
| 24 | |
| 25 | mod at1; |
| 26 | |
| 27 | use at1::case::{parse_case, CONTRACT_COMMIT}; |
| 28 | use at1::result::{compute, result_bytes, PROVENANCE_KEYS}; |
| 29 | use at1::sha256::sha256_hex; |
| 30 | use std::io::Write; |
| 31 | use std::process::Command; |
| 32 | use std::time::{SystemTime, UNIX_EPOCH}; |
| 33 | |
| 34 | fn git(args: &[&str]) -> Option<String> { |
| 35 | let dir = std::env::current_exe().ok()?.parent()?.parent()?.to_path_buf(); |
| 36 | let out = Command::new("git").args(args).current_dir(dir).output().ok()?; |
| 37 | if out.status.success() { |
| 38 | Some(String::from_utf8_lossy(&out.stdout).trim().to_string()) |
| 39 | } else { |
| 40 | None |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | /// UTC timestamp at second resolution without any calendar library. |
| 45 | fn utc_now() -> String { |
| 46 | let secs = SystemTime::now().duration_since(UNIX_EPOCH).map(|d| d.as_secs() as i64).unwrap_or(0); |
| 47 | let (days, rem) = (secs.div_euclid(86400), secs.rem_euclid(86400)); |
| 48 | let (h, m, s) = (rem / 3600, (rem % 3600) / 60, rem % 60); |
| 49 | // civil from days (Howard Hinnant) |
| 50 | let z = days + 719468; |
| 51 | let era = z.div_euclid(146097); |
| 52 | let doe = z.rem_euclid(146097); |
| 53 | let yoe = (doe - doe / 1460 + doe / 36524 - doe / 146096) / 365; |
| 54 | let y = yoe + era * 400; |
| 55 | let doy = doe - (365 * yoe + yoe / 4 - yoe / 100); |
| 56 | let mp = (5 * doy + 2) / 153; |
| 57 | let d = doy - (153 * mp + 2) / 5 + 1; |
| 58 | let mo = if mp < 10 { mp + 3 } else { mp - 9 }; |
| 59 | let y = if mo <= 2 { y + 1 } else { y }; |
| 60 | format!("{:04}-{:02}-{:02}T{:02}:{:02}:{:02}Z", y, mo, d, h, m, s) |
| 61 | } |
| 62 | |
| 63 | fn run_text(cmd: &str, args: &[&str]) -> Option<String> { |
| 64 | let o = Command::new(cmd).args(args).output().ok().filter(|o| o.status.success())?; |
| 65 | let s = String::from_utf8_lossy(&o.stdout).split_whitespace().collect::<Vec<_>>().join(" "); |
| 66 | if s.is_empty() { None } else { Some(s) } |
| 67 | } |
| 68 | |
| 69 | /// `host` (AT1_RESULT_V1 section 7: free text that, for a replication run, names the |
| 70 | /// machine and the OS): host name from /etc/hostname (Linux) or `uname -n` (macOS has no |
| 71 | /// /etc/hostname), then the OS product line from `sw_vers` when it exists (macOS) and |
| 72 | /// `uname -srm`. Whitespace runs collapse to one space; anything outside 0x20..0x7E becomes |
| 73 | /// `_`; at most 200 bytes. |
| 74 | fn host() -> String { |
| 75 | let name = std::fs::read_to_string("/etc/hostname").ok().map(|s| s.trim().to_string()).filter(|s| !s.is_empty()).or_else(|| run_text("uname", &["-n"])).unwrap_or_else(|| "unknown".to_string()); |
| 76 | let mut parts = vec![name]; |
| 77 | if let (Some(p), Some(v)) = (run_text("sw_vers", &["-productName"]), run_text("sw_vers", &["-productVersion"])) { |
| 78 | parts.push(format!("{} {}", p, v)); |
| 79 | } |
| 80 | if let Some(u) = run_text("uname", &["-srm"]) { |
| 81 | parts.push(u); |
| 82 | } |
| 83 | let raw = parts.join(" ").split_whitespace().collect::<Vec<_>>().join(" "); |
| 84 | let mut clean: String = raw.chars().map(|c| if (' '..='~').contains(&c) { c } else { '_' }).collect(); |
| 85 | clean.truncate(200); |
| 86 | let clean = clean.trim().to_string(); |
| 87 | if clean.is_empty() { "unknown".to_string() } else { clean } |
| 88 | } |
| 89 | |
| 90 | fn exe_sha256() -> String { |
| 91 | std::env::current_exe().ok().and_then(|p| std::fs::read(p).ok()).map(|b| sha256_hex(&b)).unwrap_or_else(|| "0".repeat(64)) |
| 92 | } |
| 93 | |
| 94 | fn emit(args: &[String]) -> i32 { |
| 95 | let out_path = args.iter().position(|a| a == "-o").and_then(|i| args.get(i + 1)).cloned(); |
| 96 | let inputs: Vec<&String> = args.iter().filter(|a| !a.starts_with('-') && Some(*a) != out_path.as_ref()).collect(); |
| 97 | if inputs.len() != 1 { |
| 98 | eprintln!("usage: at1-oracle emit <case-file> [-o <out-file>]"); |
| 99 | return 1; |
| 100 | } |
| 101 | let started = utc_now(); |
| 102 | let data = match std::fs::read(inputs[0]) { |
| 103 | Ok(d) => d, |
| 104 | Err(e) => { |
| 105 | eprintln!("cannot read {}: {}", inputs[0], e); |
| 106 | return 1; |
| 107 | } |
| 108 | }; |
| 109 | let case = match parse_case(&data) { |
| 110 | Ok(c) => c, |
| 111 | Err(r) => { |
| 112 | eprintln!("AT1_CASE_REFUSED {}", r.code); |
| 113 | return 2; |
| 114 | } |
| 115 | }; |
| 116 | let computed = compute(&case); |
| 117 | // source provenance is the state the binary was BUILT from (AT0_RESULT_V2 section 7), |
| 118 | // captured by build.sh; a binary built without build.sh falls back to the checkout it |
| 119 | // runs from and never claims a clean tree |
| 120 | let (commit, clean) = match (option_env!("AT1_SOURCE_COMMIT"), option_env!("AT1_SOURCE_CLEAN")) { |
| 121 | (Some(c), Some(k)) if c.len() == 40 => (Some(c.to_string()), k == "YES"), |
| 122 | // built by build.sh outside a git checkout: no commit to name (all zeros, NO) |
| 123 | (Some(_), Some(_)) => (None, false), |
| 124 | _ => (git(&["rev-parse", "HEAD"]), false), |
| 125 | }; |
| 126 | let exe = exe_sha256(); |
| 127 | let rustc = option_env!("AT1_RUSTC_VERSION").unwrap_or("rustc unknown (built without build.sh)"); |
| 128 | let flags = option_env!("AT1_BUILD_FLAGS").unwrap_or("unknown"); |
| 129 | let values: Vec<String> = vec![ |
| 130 | "aien-dev/omega".to_string(), |
| 131 | commit.clone().unwrap_or_else(|| "0".repeat(40)), |
| 132 | if commit.is_some() && clean { "YES" } else { "NO" }.to_string(), |
| 133 | CONTRACT_COMMIT.to_string(), |
| 134 | exe.clone(), |
| 135 | "aien-dev/omega".to_string(), |
| 136 | commit.unwrap_or_else(|| "0".repeat(40)), |
| 137 | exe, |
| 138 | format!("oracle {} {}", rustc, option_env!("AT1_RUSTC_HOST").unwrap_or("unknown-host")), |
| 139 | format!("rustc {} src/main.rs", flags), |
| 140 | host(), |
| 141 | started, |
| 142 | utc_now(), |
| 143 | ]; |
| 144 | let prov: Vec<(&str, String)> = PROVENANCE_KEYS.iter().cloned().zip(values).collect(); |
| 145 | let record = match result_bytes(&case, &computed, &prov) { |
| 146 | Ok(r) => r, |
| 147 | Err(e) => { |
| 148 | eprintln!("internal: {}", e); |
| 149 | return 1; |
| 150 | } |
| 151 | }; |
| 152 | match out_path { |
| 153 | Some(p) => std::fs::write(&p, &record).map(|_| 0).unwrap_or_else(|e| { |
| 154 | eprintln!("cannot write {}: {}", p, e); |
| 155 | 1 |
| 156 | }), |
| 157 | None => std::io::stdout().write_all(&record).map(|_| 0).unwrap_or(1), |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | fn fixtures(dir: &str) -> i32 { |
| 162 | if let Err(e) = std::fs::create_dir_all(dir) { |
| 163 | eprintln!("cannot create {}: {}", dir, e); |
| 164 | return 1; |
| 165 | } |
| 166 | let rendered = at1::fixtures::render(); |
| 167 | for (name, bytes) in &rendered.files { |
| 168 | if let Err(e) = std::fs::write(format!("{}/{}", dir, name), bytes) { |
| 169 | eprintln!("cannot write {}: {}", name, e); |
| 170 | return 1; |
| 171 | } |
| 172 | } |
| 173 | println!("wrote {} files to {}", rendered.files.len(), dir); |
| 174 | for line in &rendered.summary { |
| 175 | println!("{}", line); |
| 176 | } |
| 177 | 0 |
| 178 | } |
| 179 | |
| 180 | fn check(path: &str) -> i32 { |
| 181 | let data = match std::fs::read(path) { |
| 182 | Ok(d) => d, |
| 183 | Err(e) => { |
| 184 | eprintln!("cannot read {}: {}", path, e); |
| 185 | return 1; |
| 186 | } |
| 187 | }; |
| 188 | match parse_case(&data) { |
| 189 | Ok(c) => { |
| 190 | println!("case_id {}\nacceptance_id {}\ncase_file_sha256 {}", c.case_id, c.acceptance_id, c.file_sha256); |
| 191 | 0 |
| 192 | } |
| 193 | Err(r) => { |
| 194 | eprintln!("AT1_CASE_REFUSED {}", r.code); |
| 195 | 2 |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | fn main() { |
| 201 | let args: Vec<String> = std::env::args().skip(1).collect(); |
| 202 | let code = match args.first().map(String::as_str) { |
| 203 | Some("emit") => emit(&args[1..]), |
| 204 | Some("fixtures") if args.len() == 2 => fixtures(&args[1]), |
| 205 | Some("check") if args.len() == 2 => check(&args[1]), |
| 206 | _ => { |
| 207 | eprintln!("usage: at1-oracle emit <case> [-o out] | fixtures <dir> | check <case>"); |
| 208 | 1 |
| 209 | } |
| 210 | }; |
| 211 | std::process::exit(code); |
| 212 | } |
| 213 | |
| 214 | #[cfg(test)] |
| 215 | mod tests; |