interplane.Get Omega
Read in your language

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

branch main Branches Tags History

aien-sovereign-core-mv / crates / aien-runtime / tests / compose_env_test.rs

148 lines · 5022 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//! Startup refusal of compose limit settings, read from the environment the
17//! way the daemon reads them at start (`compose_budgets_from_env`,
18//! `compose_doc_max_tokens_from_env`). Its own test binary, because it sets
19//! process environment variables; every test holds `ENV` while it does.
20use aien_runtime::compose::{compose_budgets_from_env, compose_doc_max_tokens_from_env};
21use std::sync::{Mutex, MutexGuard};
22use std::time::Duration;
23
24const EDIT: &str = "AIEN_COMPOSE_EDIT_BUDGET_MS";
25const DOC: &str = "AIEN_COMPOSE_DOC_BUDGET_MS";
26const TOKENS: &str = "AIEN_COMPOSE_DOC_MAX_TOKENS";
27const RETIRED: &str = "AIEN_COMPOSE_BUDGET_MS";
28const ALL: [&str; 4] = [EDIT, DOC, TOKENS, RETIRED];
29
30/// The environment is process-global: one test at a time touches it.
31static ENV: Mutex<()> = Mutex::new(());
32
33/// Holds `ENV` with every compose limit variable unset; unsets them again when dropped.
34struct CleanEnv {
35 _guard: MutexGuard<'static, ()>,
36}
37
38fn clean_env() -> CleanEnv {
39 let g = ENV.lock().unwrap_or_else(|e| e.into_inner());
40 for k in ALL {
41 std::env::remove_var(k);
42 }
43 CleanEnv { _guard: g }
44}
45
46impl Drop for CleanEnv {
47 fn drop(&mut self) {
48 for k in ALL {
49 std::env::remove_var(k);
50 }
51 }
52}
53
54/// `v` = None leaves `name` unset.
55fn set(name: &str, v: Option<&str>) {
56 match v {
57 Some(v) => std::env::set_var(name, v),
58 None => std::env::remove_var(name),
59 }
60}
61
62#[test]
63fn environment_limits_are_read_and_bad_ones_refused() {
64 let _env = clean_env();
65 let b = compose_budgets_from_env().expect("defaults");
66 assert_eq!(b.edit, Duration::from_secs(29));
67 assert_eq!(b.doc, Duration::from_secs(120));
68 assert_eq!(compose_doc_max_tokens_from_env(), Ok(1024));
69
70 std::env::set_var(DOC, "90000");
71 std::env::set_var(TOKENS, "512");
72 assert_eq!(
73 compose_budgets_from_env().expect("set").doc,
74 Duration::from_secs(90)
75 );
76 assert_eq!(compose_doc_max_tokens_from_env(), Ok(512));
77
78 std::env::set_var(DOC, "5");
79 assert!(compose_budgets_from_env()
80 .expect_err("out of range")
81 .contains(DOC));
82 std::env::set_var(DOC, "90000");
83 std::env::set_var(TOKENS, "0");
84 assert!(compose_doc_max_tokens_from_env().is_err());
85
86 // the retired single-budget name is refused, not ignored
87 std::env::set_var(RETIRED, "29000");
88 assert!(compose_budgets_from_env()
89 .expect_err("retired")
90 .contains("retired"));
91}
92
93/// Both budget settings: unset gives the default; 1 000..=599 000 ms is
94/// accepted; anything else is refused with a message naming the setting.
95#[test]
96fn compose_budget_settings_parse_and_refuse() {
97 let _env = clean_env();
98 // One setting at a time; the other stays unset (its default).
99 let edit = |v: Option<&str>| {
100 set(DOC, None);
101 set(EDIT, v);
102 compose_budgets_from_env().map(|b| b.edit)
103 };
104 let doc = |v: Option<&str>| {
105 set(EDIT, None);
106 set(DOC, v);
107 compose_budgets_from_env().map(|b| b.doc)
108 };
109 assert_eq!(edit(None), Ok(Duration::from_secs(29)));
110 assert_eq!(doc(None), Ok(Duration::from_secs(120)));
111 assert_eq!(edit(Some("29000")), Ok(Duration::from_secs(29)));
112 assert_eq!(doc(Some("90000")), Ok(Duration::from_secs(90)));
113 assert_eq!(doc(Some(" 1000 ")), Ok(Duration::from_secs(1)));
114 assert_eq!(doc(Some("599000")), Ok(Duration::from_millis(599_000)));
115 for bad in [
116 "",
117 "abc",
118 "-5",
119 "1.5",
120 "999",
121 "0",
122 "599001",
123 "600000",
124 "99999999999999999999",
125 ] {
126 let e = edit(Some(bad)).expect_err(bad);
127 assert!(e.contains(EDIT), "{bad}: {e}");
128 let e = doc(Some(bad)).expect_err(bad);
129 assert!(e.contains(DOC), "{bad}: {e}");
130 }
131}
132
133/// AIEN_COMPOSE_DOC_MAX_TOKENS: default 1024, 16..=4096, bad values refused.
134#[test]
135fn compose_doc_token_cap_parses_and_refuses() {
136 let _env = clean_env();
137 let cap = |v: Option<&str>| {
138 set(TOKENS, v);
139 compose_doc_max_tokens_from_env()
140 };
141 assert_eq!(cap(None), Ok(1024));
142 assert_eq!(cap(Some("16")), Ok(16));
143 assert_eq!(cap(Some(" 4096 ")), Ok(4096));
144 for bad in ["", "x", "-1", "15", "0", "4097", "1e3"] {
145 let e = cap(Some(bad)).expect_err(bad);
146 assert!(e.contains(TOKENS), "{bad}: {e}");
147 }
148}