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-runtime / tests / desk_required_server_test.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 | //! sc#328 (Drake decision b, 2026-10-08): the server's own startup check. A |
| 17 | //! compose bridge that requires the approval desk (the default) refuses to |
| 18 | //! start without a loadable desk key, before the socket exists; with the key |
| 19 | //! it serves. Covers both bridge sources: a preset bridge and the one the |
| 20 | //! daemon builds from AIEN_COMPOSE_DIR. A preset bridge built with the desk |
| 21 | //! off is the dev-only opt-out: a strict run (release qualification) refuses |
| 22 | //! it, a dev run serves. No composition library is needed (the |
| 23 | //! check runs before any compose work), so this runs in every build. |
| 24 | //! |
| 25 | //! One test: AIEN_COMPOSE_DIR is process-wide. |
| 26 | use aien_inference_abi::MockInferenceBackend; |
| 27 | use aien_kv_cache::create_shared_kv_manager; |
| 28 | use aien_runtime::approved_auth::{desk_key_path, DeskKey}; |
| 29 | use aien_runtime::client::AienRuntimeClient; |
| 30 | use aien_runtime::compose::{ComposeBridge, ComposeProposer, AUTHORIZE_DESK_ENV}; |
| 31 | use aien_runtime::control::ControlCommand; |
| 32 | use aien_runtime::server::AienRuntimeServer; |
| 33 | use aien_runtime::spine::AienRuntimeSpine; |
| 34 | use aien_scheduler::SchedulerConfig; |
| 35 | use std::path::Path; |
| 36 | use std::sync::Arc; |
| 37 | use std::time::Duration; |
| 38 | |
| 39 | fn server(socket: &Path, preset: Option<&Path>, desk: bool) -> AienRuntimeServer { |
| 40 | let cfg = SchedulerConfig { |
| 41 | max_batch_size: 8, |
| 42 | max_batch_tokens: 1024, |
| 43 | prefill_chunk_size: 64, |
| 44 | watermark_blocks: 4, |
| 45 | chunk_prefill: true, |
| 46 | max_prefill_tokens: 1024, |
| 47 | }; |
| 48 | let spine = AienRuntimeSpine::new(64, cfg, create_shared_kv_manager(256, 16)); |
| 49 | let s = AienRuntimeServer::new(spine, socket); |
| 50 | match preset { |
| 51 | Some(dir) => { |
| 52 | let never: ComposeProposer = |
| 53 | Arc::new(|_: &str, _: Duration| Err("no proposer in this test".to_string())); |
| 54 | s.with_compose_bridge(Arc::new( |
| 55 | ComposeBridge::new(dir.to_path_buf(), never, "test:none") |
| 56 | .with_authorize_requires_desk(desk), |
| 57 | )) |
| 58 | } |
| 59 | None => s, |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | /// `run` must return the refusal on its own, without binding the socket. |
| 64 | async fn refused(socket: &Path, preset: Option<&Path>, compose: &Path) { |
| 65 | let s = server(socket, preset, true); |
| 66 | let e = tokio::time::timeout(Duration::from_secs(30), s.run(MockInferenceBackend::new(1))) |
| 67 | .await |
| 68 | .expect("the daemon did not refuse within 30 s") |
| 69 | .expect_err("the daemon started without the desk key"); |
| 70 | assert!(e.contains("desk-key --create"), "{e}"); |
| 71 | assert!( |
| 72 | e.contains(&desk_key_path(compose).display().to_string()), |
| 73 | "{e}" |
| 74 | ); |
| 75 | assert!(!socket.exists(), "socket bound before the refusal"); |
| 76 | assert!( |
| 77 | !desk_key_path(compose).exists(), |
| 78 | "startup must never mint a key" |
| 79 | ); |
| 80 | } |
| 81 | |
| 82 | /// With the key the daemon serves; then it is shut down cleanly. |
| 83 | async fn serves(socket: &Path, preset: Option<&Path>, desk: bool) { |
| 84 | let s = server(socket, preset, desk); |
| 85 | let h = tokio::spawn(async move { s.run(MockInferenceBackend::new(1)).await }); |
| 86 | let c = AienRuntimeClient::new(socket); |
| 87 | let mut alive = false; |
| 88 | for _ in 0..250 { |
| 89 | if c.is_alive().await { |
| 90 | alive = true; |
| 91 | break; |
| 92 | } |
| 93 | if h.is_finished() { |
| 94 | panic!("daemon stopped: {:?}", h.await); |
| 95 | } |
| 96 | tokio::time::sleep(Duration::from_millis(20)).await; |
| 97 | } |
| 98 | assert!(alive, "daemon did not come up"); |
| 99 | let _ = c.send_command(ControlCommand::Shutdown).await; |
| 100 | let _ = tokio::time::timeout(Duration::from_secs(5), h).await; |
| 101 | } |
| 102 | |
| 103 | #[tokio::test(flavor = "multi_thread", worker_threads = 2)] |
| 104 | async fn a_bridge_that_requires_the_desk_refuses_to_start_without_its_key() { |
| 105 | let tmp = tempfile::tempdir().unwrap(); |
| 106 | let root = std::fs::canonicalize(tmp.path()).unwrap(); |
| 107 | std::env::remove_var(AUTHORIZE_DESK_ENV); |
| 108 | |
| 109 | // A preset bridge, desk required by default. |
| 110 | let pre = root.join("preset"); |
| 111 | refused(&root.join("a.sock"), Some(&pre), &pre).await; |
| 112 | DeskKey::create(&desk_key_path(&pre)).unwrap(); |
| 113 | serves(&root.join("b.sock"), Some(&pre), true).await; |
| 114 | |
| 115 | // The daemon's own bridge from AIEN_COMPOSE_DIR. |
| 116 | let env = root.join("env"); |
| 117 | std::env::set_var("AIEN_COMPOSE_DIR", &env); |
| 118 | refused(&root.join("c.sock"), None, &env).await; |
| 119 | DeskKey::create(&desk_key_path(&env)).unwrap(); |
| 120 | serves(&root.join("d.sock"), None, true).await; |
| 121 | } |
| 122 | |
| 123 | #[tokio::test(flavor = "multi_thread", worker_threads = 2)] |
| 124 | async fn a_bridge_with_the_desk_off_is_refused_in_a_strict_run() { |
| 125 | let tmp = tempfile::tempdir().unwrap(); |
| 126 | let root = std::fs::canonicalize(tmp.path()).unwrap(); |
| 127 | let off = root.join("off"); |
| 128 | let socket = root.join("e.sock"); |
| 129 | if aien_inference_abi::strict::dev_fallback_active() { |
| 130 | // A dev run: the opt-out serves (and says so at startup). |
| 131 | serves(&socket, Some(&off), false).await; |
| 132 | return; |
| 133 | } |
| 134 | let s = server(&socket, Some(&off), false); |
| 135 | let e = tokio::time::timeout(Duration::from_secs(30), s.run(MockInferenceBackend::new(1))) |
| 136 | .await |
| 137 | .expect("the daemon did not refuse within 30 s") |
| 138 | .expect_err("a strict run served with the desk off"); |
| 139 | assert!(e.contains("release qualification"), "{e}"); |
| 140 | assert!(!socket.exists(), "socket bound before the refusal"); |
| 141 | } |