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-serve-http / src / lib.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 | //! `aien-serve-http`: the OpenAI-compatible HTTP door for AIEN's own-kernel model |
| 17 | //! serving (OWN-30B cut 4). Loopback only, one generation at a time unless the engine has more slots (--batch-slots), no CUDA, no |
| 18 | //! Python. The engine behind the door is a trait ([`engine::Engine`]); this crate |
| 19 | //! ships a stub engine for tests and the CPU reference forward as a real-but-slow |
| 20 | //! engine. The Omega GPU engine plugs into the same trait. |
| 21 | |
| 22 | pub mod chip_reservation; |
| 23 | pub mod codec; |
| 24 | pub mod cpu_engine; |
| 25 | pub mod engine; |
| 26 | pub mod kv_persist; |
| 27 | pub mod omega_engine; |
| 28 | pub mod openai; |
| 29 | pub mod qwen36_batch; |
| 30 | pub mod qwen38_engine; |
| 31 | pub mod readers; |
| 32 | pub mod sampling; |
| 33 | pub mod server; |
| 34 | pub mod text; |
| 35 | pub mod tools; |
| 36 | |
| 37 | pub use codec::{ByteCodec, Codec, HfCodec}; |
| 38 | pub use engine::{Engine, EngineError, FinishReason, GenerateOutcome, GenerateParams, StubEngine}; |
| 39 | pub use server::{serve, ServerConfig}; |
| 40 | |
| 41 | /// Largest small JSON file (`config.json`, `generation_config.json`) read from the model |
| 42 | /// directory; the same bound as the checkpoint loader's shard index. |
| 43 | pub const MODEL_JSON_LIMIT: u64 = 16 << 20; |
| 44 | |
| 45 | /// Read a small text file from the model directory with at most `MODEL_JSON_LIMIT + 1` |
| 46 | /// bytes pulled. Refuses a non-regular file (a FIFO or device would block or never end) and |
| 47 | /// a file over the limit. A missing file is `ErrorKind::NotFound`, as with `fs::read_to_string`. |
| 48 | pub fn read_model_text(path: &std::path::Path) -> std::io::Result<String> { |
| 49 | use std::io::{Error, ErrorKind}; |
| 50 | use std::os::unix::fs::OpenOptionsExt; |
| 51 | let file = std::fs::OpenOptions::new() |
| 52 | .read(true) |
| 53 | .custom_flags(libc::O_NONBLOCK) |
| 54 | .open(path)?; |
| 55 | if !file.metadata()?.is_file() { |
| 56 | return Err(Error::new(ErrorKind::InvalidInput, "not a regular file")); |
| 57 | } |
| 58 | read_text_capped(file, MODEL_JSON_LIMIT) |
| 59 | } |
| 60 | |
| 61 | fn read_text_capped(src: impl std::io::Read, limit: u64) -> std::io::Result<String> { |
| 62 | use std::io::{Error, ErrorKind, Read}; |
| 63 | let mut text = String::new(); |
| 64 | src.take(limit + 1).read_to_string(&mut text)?; |
| 65 | if text.len() as u64 > limit { |
| 66 | return Err(Error::new( |
| 67 | ErrorKind::InvalidData, |
| 68 | format!("file exceeds {limit} bytes"), |
| 69 | )); |
| 70 | } |
| 71 | Ok(text) |
| 72 | } |
| 73 | |
| 74 | #[cfg(test)] |
| 75 | mod model_text_tests { |
| 76 | use super::*; |
| 77 | use std::io::Read; |
| 78 | |
| 79 | /// An endless source that panics once more than `budget` bytes are pulled. |
| 80 | struct Endless { |
| 81 | served: u64, |
| 82 | budget: u64, |
| 83 | } |
| 84 | |
| 85 | impl Read for Endless { |
| 86 | fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> { |
| 87 | buf.fill(b' '); |
| 88 | self.served += buf.len() as u64; |
| 89 | // Plain message: CodeQL rust/cleartext-logging (alert 102) conflated this |
| 90 | // Read impl with spark-mail-rs `vault_password` and flagged formatted values. |
| 91 | assert!(self.served <= self.budget, "read past the cap"); |
| 92 | Ok(buf.len()) |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | #[test] |
| 97 | fn capped_read_stops_one_byte_past_the_limit() { |
| 98 | let mut src = Endless { |
| 99 | served: 0, |
| 100 | budget: 4097, |
| 101 | }; |
| 102 | let err = read_text_capped(&mut src, 4096).unwrap_err(); |
| 103 | assert_eq!(err.kind(), std::io::ErrorKind::InvalidData); |
| 104 | assert_eq!(src.served, 4097); |
| 105 | } |
| 106 | |
| 107 | #[test] |
| 108 | fn model_text_reads_the_limit_and_refuses_more_and_non_regular_files() { |
| 109 | let dir = tempfile::tempdir().unwrap(); |
| 110 | let p = dir.path().join("config.json"); |
| 111 | let mut body = vec![b' '; MODEL_JSON_LIMIT as usize - 2]; |
| 112 | body.splice(0..0, *b"{}"); |
| 113 | std::fs::write(&p, &body).unwrap(); |
| 114 | assert_eq!(read_model_text(&p).unwrap().len() as u64, MODEL_JSON_LIMIT); |
| 115 | |
| 116 | body.push(b' '); |
| 117 | std::fs::write(&p, &body).unwrap(); |
| 118 | assert_eq!( |
| 119 | read_model_text(&p).unwrap_err().kind(), |
| 120 | std::io::ErrorKind::InvalidData |
| 121 | ); |
| 122 | |
| 123 | assert_eq!( |
| 124 | read_model_text(&dir.path().join("absent.json")) |
| 125 | .unwrap_err() |
| 126 | .kind(), |
| 127 | std::io::ErrorKind::NotFound |
| 128 | ); |
| 129 | // /dev/zero never ends; it is refused before any byte is read. |
| 130 | assert_eq!( |
| 131 | read_model_text(std::path::Path::new("/dev/zero")) |
| 132 | .unwrap_err() |
| 133 | .kind(), |
| 134 | std::io::ErrorKind::InvalidInput |
| 135 | ); |
| 136 | } |
| 137 | } |