lean_ctx/core/intelligence_runtime/
mod.rs1mod bootstrap;
6mod catalog;
7mod channel;
8mod cli;
9pub mod cockpit;
10pub mod code_security;
11pub mod compliance_frameworks;
12mod configuration;
13pub(crate) mod dashboard_pro;
14mod exchange;
15pub(crate) mod org_policy_verifier;
16pub(crate) mod semantic_coverage;
17pub mod semantic_detectors;
18
19#[cfg(any(test, feature = "test-support"))]
21#[cfg_attr(
22 not(any(target_os = "macos", all(target_os = "linux", target_env = "gnu"))),
23 allow(dead_code)
24)]
25pub(crate) fn private_root_for_test() -> tempfile::TempDir {
26 let mut builder = tempfile::Builder::new();
27 builder.prefix("runtime-install-test-");
28 #[cfg(unix)]
29 {
30 use std::os::unix::fs::PermissionsExt;
31 builder.permissions(std::fs::Permissions::from_mode(0o700));
32 }
33 builder.tempdir().expect("private test root")
34}
35
36#[cfg(all(
38 any(test, feature = "test-support"),
39 any(target_os = "macos", all(target_os = "linux", target_env = "gnu"))
40))]
41pub(crate) fn sign_document(
42 document: &serde_json::Value,
43 key: &ed25519_dalek::SigningKey,
44 domain: &[u8],
45) -> (Vec<u8>, Vec<u8>, String) {
46 use ed25519_dalek::Signer;
47 let manifest = serde_json::to_vec(document).expect("manifest");
48 let mut payload = domain.to_vec();
49 payload.extend_from_slice(&manifest);
50 let signature = key.sign(&payload).to_bytes().to_vec();
51 let selected = sha256(&manifest);
52 (manifest, signature, selected)
53}
54
55#[cfg(all(
58 feature = "test-support",
59 not(test),
60 any(target_os = "macos", all(target_os = "linux", target_env = "gnu"))
61))]
62#[allow(dead_code, unused_imports, clippy::unwrap_used)]
63#[path = "company_journey_tests.rs"]
64pub mod company_journey_support;
65mod semantic_worker;
66pub use configuration::{
67 COMPANIONS_LATER, DISCLOSURE, SetupRuntime, inspect_setup, load_companions,
68};
69mod health;
70mod install;
71mod license;
72pub mod lease;
74mod manifest;
75mod rotation;
76#[cfg(any(unix, windows))]
77mod session;
78#[cfg(windows)]
79mod session_windows;
80
81#[cfg(test)]
82pub(crate) mod tests;
83
84use std::io::Read;
85use std::path::Path;
86
87pub use cli::{USAGE, error_message, human_note, plain_install, run};
88use manifest::{VerifiedPackage, verify, verify_kind};
89
90const EXECUTABLE_NAME: &str = if cfg!(windows) {
92 "leanctx-intelligence.exe"
93} else {
94 "leanctx-intelligence"
95};
96
97#[derive(Debug, Clone, Copy, PartialEq, Eq)]
103pub(crate) enum RuntimeKind {
104 Intelligence,
106 Cockpit,
108}
109
110impl RuntimeKind {
111 pub(crate) const fn engine_id(self) -> &'static str {
112 match self {
113 Self::Intelligence => "leanctx-intelligence",
114 Self::Cockpit => "leanctx-cockpit",
115 }
116 }
117
118 pub(crate) const fn store_name(self) -> &'static str {
121 match self {
122 Self::Intelligence => "intelligence-runtime",
123 Self::Cockpit => "cockpit-runtime",
124 }
125 }
126}
127
128#[derive(Debug, thiserror::Error)]
129pub enum InstallError {
130 #[error("invalid runtime installer arguments")]
131 Usage,
132 #[error("runtime download URL, transport or response rejected")]
133 Download,
134 #[error("runtime package I/O failed")]
135 Io(#[from] std::io::Error),
136 #[error("runtime metadata or package exceeds its size bound")]
137 Size,
138 #[error("runtime input is not a regular, non-symlink file")]
139 Input,
140 #[error("runtime package manifest is not independently selected or compatible")]
142 Manifest,
143 #[error("runtime channel catalog has expired; try again later or update lean-ctx")]
146 ChannelExpired,
147 #[error("runtime channel catalog does not match the channel this LeanCTX version accepts")]
150 ChannelMismatch,
151 #[error("Runtime for this platform is not available yet")]
153 PlatformUnavailable,
154 #[error("Runtime is not offered by this build")]
157 NotOffered,
158 #[error("runtime signature does not match the independent trust key")]
159 Signature,
160 #[error("runtime archive is invalid or incompatible with this host")]
161 Archive,
162 #[error("runtime installation directory is not private and canonical")]
163 Directory,
164 #[error("another runtime installation is active")]
165 Busy,
166 #[error("runtime selection changed or retained package is inconsistent")]
167 State,
168 #[error("runtime channel is older than the active one; a downgrade must be requested")]
169 Downgrade,
170 #[error("atomic runtime selection write failed")]
171 Commit,
172 #[error("runtime health check failed or exceeded its bound")]
173 Health,
174 #[error("runtime invocation is not supported by the authenticated package and this host")]
175 InvocationAdmission,
176 #[error("runtime invocation failed or exceeded its bound")]
177 Exchange,
178 #[error("runtime request or policy input was rejected")]
179 Policy,
180 #[error("runtime global configuration could not be updated")]
181 Configuration,
182}
183
184pub(crate) type Result<T> = std::result::Result<T, InstallError>;
185
186fn trust_key(encoded: &str) -> Result<[u8; 32]> {
187 if !is_digest(encoded, 64) {
188 return Err(InstallError::Usage);
189 }
190 let mut key = [0; 32];
191 for (index, byte) in key.iter_mut().enumerate() {
192 *byte = u8::from_str_radix(&encoded[index * 2..index * 2 + 2], 16)
193 .map_err(|_| InstallError::Usage)?;
194 }
195 Ok(key)
196}
197
198pub(crate) fn read_regular(path: &Path, limit: usize) -> Result<Vec<u8>> {
200 let metadata = std::fs::symlink_metadata(path)?;
201 if !regular_metadata(&metadata) {
202 return Err(InstallError::Input);
203 }
204 let mut options = std::fs::OpenOptions::new();
205 options.read(true);
206 #[cfg(unix)]
207 {
208 use std::os::unix::fs::OpenOptionsExt;
209 options.custom_flags(libc::O_NOFOLLOW | libc::O_NONBLOCK);
210 }
211 #[cfg(windows)]
212 {
213 use std::os::windows::fs::OpenOptionsExt;
214 use windows_sys::Win32::Storage::FileSystem::FILE_FLAG_OPEN_REPARSE_POINT;
215 options.custom_flags(FILE_FLAG_OPEN_REPARSE_POINT);
217 }
218 let file = options.open(path)?;
219 let metadata = file.metadata()?;
220 if !regular_metadata(&metadata) {
221 return Err(InstallError::Input);
222 }
223 if metadata.len() > limit as u64 {
224 return Err(InstallError::Size);
225 }
226 let mut bytes = Vec::new();
227 file.take(limit as u64 + 1).read_to_end(&mut bytes)?;
228 if bytes.len() > limit {
229 return Err(InstallError::Size);
230 }
231 Ok(bytes)
232}
233
234fn regular_metadata(metadata: &std::fs::Metadata) -> bool {
235 #[cfg(windows)]
236 {
237 use std::os::windows::fs::MetadataExt;
238 use windows_sys::Win32::Storage::FileSystem::FILE_ATTRIBUTE_REPARSE_POINT;
239 if metadata.file_attributes() & FILE_ATTRIBUTE_REPARSE_POINT != 0 {
241 return false;
242 }
243 }
244 metadata.is_file() && !metadata.file_type().is_symlink()
245}
246
247use super::updater::sha256_hex as sha256;
248
249pub(crate) fn is_digest(value: &str, length: usize) -> bool {
250 value.len() == length
251 && value
252 .bytes()
253 .all(|byte| byte.is_ascii_digit() || (b'a'..=b'f').contains(&byte))
254}